paperstreet7
paperstreet7

Reputation: 2188

is any JavaScript statement an expression?

I know that functional languages like Lisp don't have statements. Everything there is an expression. JavaScript is a functional language. So I came to a conclusion that every JavaScript statement is an expression. This thought came to my mind when I was playing with chrome's console. Every statement entered there is evaluated and the console returns undefined if an expression doesn't return certain value.

Upvotes: 3

Views: 254

Answers (2)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123463

I'd say no, as you can't use just any statement where an expression is expected:

// SyntaxError: Unexpected token var
var a = var b;
// SyntaxError: Unexpected token if
var c = if (true) {};

The undefined shown in Chrome's console is due to its use of eval() (or a native/internal equivalent), which evaluates any code:

var a = eval('var b;');
console.log(a); // undefined

The undefined isn't the result of var b;, but because eval() itself still has a return value -- whether the evaluated code supplied it or not.

Upvotes: 5

Bergi
Bergi

Reputation: 664484

I came to a conclusion that every JavaScript statement is an expression

No, definitely not. The EcmaScript standard differentiates quite clearly between Statements (§12) and Expressions (§11).

Yet, what you are mistaking for expressions is the Expression Statement (§12.4), which consists solely of an expression (and is delimited by a semicolon).

Upvotes: 7

Related Questions