Value returned by the assignment

JavascriptAssignment Operator

Javascript Problem Overview


Why does the regular assignment statement (say, x = 5) return the value assigned (5 in this case), while the assignment combined with a variable declaration (var x = 5) returns undefined?

I got the return values by executing these statements in the Chrome browser's Javascript console:

> var x = 5;
undefined
> y = 5;
5

Javascript Solutions


Solution 1 - Javascript

That's the way the language was designed. It is consistent with most languages.

Having a variable declaration return anything other than undefined is meaningless, because you can't ever use the var keyword in an expression context.

Having assignment be an expression not a statement is useful when you want to set many variable to the same value at once:

x = y = z = 2;

It can also be used like this:

x = 2*(y = z); // Set y = z, and x = 2*z

However that is not the most readable code and it would probably be better written as:

y = z;
x = 2*z;

Solution 2 - Javascript

That's because var x = 5; is a variable statement, not an expression.

The behaviour of this statement is described in Section 12.2 of the ECMAScript Language Reference.

> 1. Evaluate VariableDeclarationList. 2. Return (normal, empty, empty).

This is basically a void return value.

Solution 3 - Javascript

The assignment operator (i.e., the equals sign) (1) assigns the right-side-operand (i.e., a value or the value of a variable, property, or function) to the left-side-operand (i.e., variable or property) and then (2) the assignment expression (e.g., y = 10) becomes a simple operand with the same value as its right-side-operand (e.g., 10) before the rest of the expression is evaluated. This is similar to when a called function is replaced with its return value when an expression is evaluated (although function calls are first in the order of operations and assignment operations are fourteenth):

var x, y, z = 1;
x = z + (y = 2); // x === 3     

function returnTwo () {
    return 2;
}

x = z + returnTwo(); // x === 3

Take note that not only does x now equal 3, but the entire expression evaluates to 3.

The purpose of the var keyword is to bind variables to the current scope. Variables declared with the var keyword are bound to the scope where they are declared. The var keyword assigns the left-most variable (or property) as a reference to the value of the evaluated expression:

var fun = function () {
    var x = 1;
    var y = x + 1; 
    return y;
}

// The x and y variables are bound to the scope of the fun function.

Using the var keyword with an expression is called a declaration. Declarations are actions that do not evaluate to a value, not even undefined (even though your console is printing undefined). Further, declarations cannot appear where JavaScript expects an expression, as other answers to this post have shown.

Solution 4 - Javascript

When you write var x = 5; it declares x and initalizes its value to 5.

This is a VariableStatement, it returns nothing,

but x=5 is an expression that assigns 5 to x. as there is no x, JavaScript implicitly creates a global x in normal code

Solution 5 - Javascript

I edited my answer because of comment and some other answers.

Assignment operator doesn't return anything... In below example, first thing JS parser does is assigning 5 to y. Second thing is assigning y to x, and so on. Assigning is not return (it's not a function, and in JS it doesn't have C++ syntax to overload operator's behavior). return is sth more complex then assignment. return construct is not only returning a value, but is closing current context causing it to be destroyed. Also it's closing any parent context (closure pattern) if there is no other child using it. So please, DO NOT tell me (in comments) that assignment operator returns any value. Assignment operator in case of JS is only a language construct.

This language construct is useful in chains (and that's why everyone is talking about returning):

a = x = y = 5;

Any undeclared variable is declared automatically by parser in global scope. If you declare variable explicitly, then you can't at the same time use it in chain, like this:

a = var x = y = 5;

Proper use of above code would be:

var x = y = 5;
a = x;

Of course you can use brackets, to make your code clearer, but it doesn't mean that code behaves like a function.

Also your example works only in JS console, which is not returning but printing the result of statement, or expression. It means that JS console treats result of declaring of variable as undefined (same when creating function: function a() {}).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser10165View Question on Stackoverflow
Solution 1 - JavascriptPaulView Answer on Stackoverflow
Solution 2 - JavascriptJa͢ckView Answer on Stackoverflow
Solution 3 - JavascriptorbView Answer on Stackoverflow
Solution 4 - JavascriptSachinView Answer on Stackoverflow
Solution 5 - JavascriptKarolView Answer on Stackoverflow