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 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
Share
Improve this question
asked Mar 25, 2015 at 14:43
simon.denelsimon.denel
7986 silver badges23 bronze badges
7
|
Show 2 more comments
2 Answers
Reset to default 21In 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);
eval
? :) This question is unclear, you mean you want to have some chars before and some after ? Why ? – Denys Séguret Commented Mar 25, 2015 at 14:45