Interesting stuff which you might have missed in Javascript
Tell me the output:
var text = ‘outside’;
function logIt() {
console.log(text);
var text = ‘inside’;
};logIt();
Output:
Undefined
Some expected the result of the log to be:’outside’ or ReferenceError: hoist is not defined
, but instead, its output is undefined
.
Reason:
JavaScript has hoisted the variable declaration.
For the interpreter, the code was like:
function logIt() {
var text;
console.log(text);
text = ‘inside’;
};
To copy an array:
var b = [1, 2, 3, 4];
var copyB = […b];
Guess the Output:
var result = 3 + “3”; //output 33
var result1 = “3” + 3; //output 33
var result2 = +”3" + 3; //output 6
var result3 = -”-3" + 3; //output 6
var result4 = -”3" + 3; //output 0