Scope
Q: What will be printed on the console?
(function () {
var a = (b = 5);
})();
console.log(b); // $: 5
- variable
a
is declared using the keywordvar
as local function variable - b is not defined using
var
and is assigned to the global scope - doesn't use strict mode (
'use strict';
) inside the function- strict mode requires you to explicitly reference to the global scope
var a = window.b = 5;
- strict mode requires you to explicitly reference to the global scope
(function () {
'use strict';
var a = (b = 5);
})();
console.log(b); // $: Uncaught ReferenceError: b is not defined