Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"var variable" returns undefined?

Tags:

javascript

When I run "var variable = true;" in chrome console I get "undefined" returned:

> var variable = true;
undefined

But when I run without "var" it returns true:

> variable = true;
true

Why is it returning "undefined" with "var"?

It's confusing cause I expected it would return true.

like image 955
never_had_a_name Avatar asked Feb 03 '23 01:02

never_had_a_name


1 Answers

The first is a statement, while the second is an expression. While not quite the same, it is similar to C's rules:

// A statement that has no value.
int x = 5;

// An expression...
x = 10;

// ...that can be passed around.
printf("%d\n", x = 15);
like image 74
Marcelo Cantos Avatar answered Feb 05 '23 14:02

Marcelo Cantos