Skip to content
Advertisement

Why some statements can’t be used with boolean operators

I like to use this style for single line conditional function calls:

JavaScript

It’s much more terse than it’s if counter part.

However some statements dont work, e.g. return and debugger:

JavaScript

Why does it not work for the above statements? Instead I use:

JavaScript

Advertisement

Answer

For the same reason you can’t say things like these:

JavaScript

Javascript, like a lot of other programming languages, retains the difference between an expression and a statement. You can’t use a statement in a place where syntactically an expression is expected.

Expressions evaluate to a value, even if that value is undefined. An invocation of a function is an expression. Even an assignment is an expression. But things like try, return, and if are statements.

In some programming languages (notably most functional languages) everything is an expression and evaluates to a value.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement