(function b() {
console.log(b);
b = 20;
console.log(b);
})();
I wrote this JavaScript IIFE.
The first console.log
logs the function body.
Then the b
variable is created with value 20
.
The second console.log
also logs the function body.
Why not 20
?
Because b
is constant and cannot be assigned to. You're in non-strict mode, so the assignment just silently does nothing, but if you use strict mode you will get an error, highlighting the problem.
(function b() {
'use strict';
console.log(b);
b = 20; // Throws an exception
console.log(b); // Never gets to here
})();