In nodeJS terminal, I can enter this expression and have as a return 'true':
> var x = true; x;
true
How can I capture this return value in a variable, without changing the expression?
The following is not working:
> var y = (var x = true; x)
SyntaxError: Unexpected token var
In node REPL, you can just use _ :
> var x = true; x;
true
> var y = _
undefined
> y
true
You can't use a statement as an expression.
x = true is an expression, and x is also an expression. var x = true is not an expression, it's a statement.
To use the expression you would declare the variable x first. The value of an assignment expression is the value that was assigned, so you don't need to put x; after the assignment (which helps as that makes it a statement):
var x; var y = (x = true);