Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript OR (||) variable assignment explanation

Given this snippet of JavaScript...

var a; var b = null; var c = undefined; var d = 4; var e = 'five';  var f = a || b || c || d || e;  alert(f); // 4 

Can someone please explain to me what this technique is called (my best guess is in the title of this question!)? And how/why it works exactly?

My understanding is that variable f will be assigned the nearest value (from left to right) of the first variable that has a value that isn't either null or undefined, but I've not managed to find much reference material about this technique and have seen it used a lot.

Also, is this technique specific to JavaScript? I know doing something similar in PHP would result in f having a true boolean value, rather than the value of d itself.

like image 477
chattsm Avatar asked Jan 20 '10 10:01

chattsm


People also ask

How do you assign JavaScript?

assign() This method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'Set' on the target. It returns the target object which has properties and values copied from the target object.

How JavaScript variables are assigned?

Variables in JavaScript are used to store data that will be used in our program. A variable can point to almost any type of value including numbers, strings, arrays, objects, and functions. Variables are assigned values using the = operator.

Does JavaScript have multiple assignment?

“JavaScript allows multiple left-hand assignments” var a = b = c = d = 10; The assignment expressions are always evaluated from right-to-left. So what the above expression actually does is, assign the value 10 to the variable d , then assign the value of d to c and so on.

What is the function of assignment operators?

An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.


2 Answers

See short-circuit evaluation for the explanation. It's a common way of implementing these operators; it is not unique to JavaScript.

like image 122
unwind Avatar answered Oct 14 '22 23:10

unwind


This is made to assign a default value, in this case the value of y, if the x variable is falsy.

The boolean operators in JavaScript can return an operand, and not always a boolean result as in other languages.

The Logical OR operator (||) returns the value of its second operand, if the first one is falsy, otherwise the value of the first operand is returned.

For example:

"foo" || "bar"; // returns "foo" false || "bar"; // returns "bar" 

Falsy values are those who coerce to false when used in boolean context, and they are 0, null, undefined, an empty string, NaN and of course false.

like image 37
Christian C. Salvadó Avatar answered Oct 15 '22 01:10

Christian C. Salvadó