loading...

What are examples of expression statements in JavaScript?

pandaquests profile image pandaquests ・1 min read

I know what an expression is and I know what a statement is in JavaScript. But what are expression statement in JavaScript?

I know that the ternary operator (... ? ... : ...) and function calls (alert("Hello World")) are expression statements. But what else? Or are only these two considered expression statements?

Discussion

markdown guide
 

An expression in Javascript (and pretty much all languages, with variation) at the most basic level is just "anything that takes on a value".

So the number 42 or the string "Hello world" or 7 + 3 because they all "take on" a value (42, Hello world, and 10).

As you can tell from this definition, almost everything is an expression. The only other "type" of thing are statements, which there's a more limited number of (depending on language). Things like if / else in Javascript or for because these things don't take on values (you couldn't write something like let variable = if (true) { ... }).