Aptitude | JavaScript Course Quiz 3 | Question 5

What will be the output of the following code?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


(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



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

About immukul

Executive Software Developer at Cavisson Systems