What will be printed to console after executing the following code?

(function(){
  var x = y = 5;
})();

console.log("x defined? " + (typeof x !== 'undefined'));
console.log("y defined? " + (typeof y !== 'undefined'));
Explanation
Since the statement var x = y = 3; is shorthand for the statements y = 3; and var x = y; y ends up being a global variable (since it is not preceded by the var keyword) and is therefore still in scope even outside of the enclosing function.

Typo : in the explanation it must be 5 and not 3.

2018 Aug 26, 3:44:42 PM

@misterds it should be right! please, run code in the browser console. Or check the explanation

2017 Dec 12, 11:36:18 AM

x defined? true y defined? true

2017 Dec 11, 6:23:41 PM

the corect unswer is

2017 Dec 11, 6:23:25 PM

@aalaminosz26 :) good intuition!

2017 Dec 5, 3:50:04 PM

I got it right for the wrong reasons.

2017 Nov 25, 8:26:10 AM

@Kot_leto it should be correct actually. Try to check it in the console ;)

2017 May 17, 11:54:39 PM

the corrent answer is: x defined? true y defined? true

2017 Jan 8, 10:47:27 AM

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з JavaScript
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести