简体   繁体   中英

What is this in javascript: “var var1 = var1 || []”

I just want to increase my core javascript knowledge.

Sometimes I see this statement but I don't know what it does:

var var1 = var1 || [];

What does it means and/or what's it for, and how do you use it?

Thank you.

Basically, it looks to see if a variable var1 already exists and is "truthy". If it is, it assigns the local var1 variable its value; if not, it gets assigned an empty array.

This works because the JavaScript || operator returns the value of the first truthy operand, or the last one, if none are truthy. var1 || var2 var1 || var2 returns var1 if it's truthy, or var2 otherwise.

Here are some examples:

var somevar;
somevar = 5 || 2; // 5
somevar = 0 || 2; // 2
somevar = 0 || null; // null

Values that aren't "truthy": false , 0 , undefined , null , "" (empty string), and NaN . Empty arrays and objects are considered truthy in JavaScript, unlike in some other languages.

如果它的布尔表示为假(例如它尚未初始化),它会为var1指定一个空数组。

基本上,如果var1NULLfalse ,则var1将设置为空array

The logical operators in JavaScript actually evaluate to one of the two objects. When you use a || b a || b it evaluates to b if a is false, or to a if a is true. Thus a || [] a || [] will be a if a is any value that is true, or [] if a is any value that is false.

It's much more obvious to use if (!a) { a = [] };

Javascript or (||) works a bit differently to some other languages, it returns the first "truthy" value instead of a boolean. This is being used in this case to say "Set the value of var1 to var1 , but if that value is "falsey" set it to [] ".

This is often used to set a "default" value to a variable that may or may not be set already, such as an argument to a function.

The || operator evaluates the first of its operands that is "truthy".

[] is an empty array. ( [ "Hi!" ] is an array of one string)

Therefore, the expression x || [] x || [] evaluates to x if it's "truthy" or an empty array if it isn't.

This allows the var1 parameter to be optional.

The statement assigns an empty array to var1.


longer answer and explanation:

This happens because var1 is not initialized at that time. Non-initialized is a falsy value.

take this statement:

var1 = var1 || [];

If var1 is not initialized it becomes an empty array, it if is an empty array nothing happens as its assigned to an empty array, if var1 is false , null , or any other value that javascript things of as false , it becomes an empty array, if var1 is anything other value, nothing happens as it is assigned to itself. (thanks pst for the link).

In short, its a stupid statement that's neither readable nor useful, but you're smart for wanting to know what it means. :)

While this has been pointed out as 'being a silly statement', I present the following two counters:

(Just to keep people on their toes and reinforce some of the "finer details" of JavaScript.)

1)

var is the variable is already local . Eg

function x (y) {
  var y = y || 42 // redeclaration warning in FF, however it's "valid"
  return y
}
x(true) // true
x()     // 42

2)

var is function-wide annotation (it is "hoisted" to the top) and not a declaration at the point of use.

function x () {
  y = true
  var y = y || 42
}
x()      // true

I do not like code like either of the preceding, but...

Because of the hoisting, and allowed re-declarations, the code in the post has these semantics:

var var1
if (!var1) {
  var1 = []
}

Edit I am not aware of how "strict mode" in Ed.5 influences the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM