Open In App

Aptitude | JavaScript Course Quiz 3 | Question 5

What will be the output of the following code?




<script>
let bar  = 1;
foo = {};
foo: {
     bar : 2;
     baz : ++bar;
};
document.write(foo.baz + foo.bar + bar);
</script>

(A) NaN
(B) 5
(C) 4
(D) 1

Answer: (A)
Explanation: It’s not actually altering the variable foo. Here the text foo is part of a label, and the object that follows is called a javascript block and it contains two labeled statements, not variable assignments. That leaves us with two undefined variables added to 1. Which is NaN. If the variable bar was not declared higher in the code this object would kick out a syntax error as bar would be undefined.
Quiz of this Question

Article Tags :