#javascript Interview question: what's the output of below code and comment out why?
@agr_manisha10 This code has a side effect: me questioning the company.
@agr_manisha10 The answer to these questions should be, I don’t want to work for a company that has code like this in their code base. Merge request rejected.
@agr_manisha10 I think the output should be 1 1 This is because the var declarations are hoisted to the top. When the function does not get a, it will look out of local scope and find a in the global scope and print that value.
@agr_manisha10 I got output as 10 and 1 can anyone make me understand why it is 10 and 1
@agr_manisha10 Due to variable hoisting, the variable 'a' will came on top of the its scope and value undefined will be stored in it. Since undefined is false statement and with ! operation its opposite will be true. Hence, the if statement will be executed and 10 will be assign to it. 10, 1.
Ah, this code? It's a delightful puzzle box of confusion! Inside the function, a plays hide-and-seek with hoisting, while outside, it proudly displays its 1. It's like a riddle wrapped in a mystery inside an enigma, all summed up in the beautiful chaos of undefinedness. Brilliant!
@agr_manisha10 Nice one. Terrible interview question though.
@agr_manisha10 Var is fuction scope so when we calling the function a is getting hoisted but its value will be undefined. Undefined is a falsy value so !a will be true so now a = 10 As a result we will get 10 and 1
@agr_manisha10 I would not expect from new hire to tell me what is the output, but explain why is code poorly written and hot to refactor it. Anyway it should be 1 and 1. Why? Check if statement.
@agr_manisha10 The output of this code will be: 10 1 #javascript #Quiz
@agr_manisha10 10. This is due to variable hoisting esp. with the identifier `var`. Variables declared outside the function are not accessible within the function but the var within the function gets hoisted up into the if statement where it has a value of undefined.
@agr_manisha10 The thing that bothers me is you using the word interview. If you ask these questions I’ll run out to keep my sanity
@agr_manisha10 1 1 will be the output of this code because if condition is never gonna be run because by default a is true which makes !a false, so it only run console.log in that function.
@agr_manisha10 function data() { var a; if (!a) { a = 10; } console.log(a); } var a; a=1 data() console.log(a) this is the way the compiler breaks it down innit. the answer will always be "10" and "1" respectively. Scope rules
@agr_manisha10 I don't know what the code does. But I commented out why. //why
@agr_manisha10 So it's print's 10. However outside the function the global variable stays unaffected so it print's 1 (2/2)
@agr_manisha10 It's giving error not printing even 1 also
@agr_manisha10 Inside the function call a change its state to be 10 because values are copied by their number/value, also the object is not stored in memory. once a goes outside of the callback it returns it value to be 1 like that of the console.log after the function call.
@agr_manisha10 Good one. I thought its 1,1. Another tricky output question I once got- Output of: {}=={}
@agr_manisha10 it give 10 and 10 because var is a local scope datatype. it can be updated and also redefined