javascript - Why is one variable undefined outside of the IIFE while the other is not? - Stack Overflow

I was playing with IIFE and I'm confused. The value of a is undefined but b is not. Why am I able

I was playing with IIFE and I'm confused. The value of a is undefined but b is not. Why am I able to access the value of b outside of IIFE?

(function(){
  var a = b = 3;
})();

console.log("a defined? " + (typeof a !== 'undefined')); // false
console.log("b defined? " + (typeof b !== 'undefined')); // true

I was playing with IIFE and I'm confused. The value of a is undefined but b is not. Why am I able to access the value of b outside of IIFE?

(function(){
  var a = b = 3;
})();

console.log("a defined? " + (typeof a !== 'undefined')); // false
console.log("b defined? " + (typeof b !== 'undefined')); // true

Share Improve this question edited Mar 9, 2019 at 15:41 Ivar 6,88712 gold badges56 silver badges67 bronze badges asked Mar 9, 2019 at 15:30 spratap124spratap124 1713 silver badges12 bronze badges 3
  • 5 b is an implicit global because it has not been declared. – Paul Commented Mar 9, 2019 at 15:32
  • 2 Should be var a = 3, b = a; – Quentin Commented Mar 9, 2019 at 15:33
  • 3 …or var a, b; a = b = 3; – Bergi Commented Mar 9, 2019 at 15:34
Add a ment  | 

2 Answers 2

Reset to default 12

Declaration syntax can be confusing, because it looks like the syntax for ordinary expressions. It is, however, different.

var a = b = 3;

is parsed as

var a = (something);

In this case, something is b = 3, so it's exactly as if your function looked like

b = 3;
var a = b;

The "naked" b = 3 without var or let or const creates an implicit global variable. Thus b is visible outside the function, while a is not.

There are four ways of declaring a variable in JavaScript:

  • var, which will scope that variable to the declaring function.
  • let/const, which will scope that variable to the declaring block.
  • Implicit declaration, which will scope that variable globally (unless previously declared in a different scope, in which case it will reassign that instead).
var a = b = 3;

In this statement, we declare a in the function scope with the value of b = 3. The expression b = 3 has value 3, but implicitly declares the variable b as well, which means that b will be declared in the global scope.

Outside of the function, the variable b is declared (since it was implicitly declared globally) while a is not (since it was declared only in the scope of the function).


You should avoid implicitly declared variables, though (and preferably use let and const instead of var), so the above code should have been written like this:

(function() {
  let a = 3;
  let b = a;
});

Or, if you actually want the variables declared outside of the function as well:

let a, b;
(function() {
  // NOTE: not implicit declaration since a and b are both already declared
  a = 3;
  b = a;
})();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744333844a4569016.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信