Javascript ternary boolean shorthand

Is there a shorthand syntax for the following JavaScript boolean ternary expression:

var foo = (expression) ? true : false

Upvotes: 1

Views: 406

Answers (1)

Bergi
Bergi

Reputation: 664548

Sure, you just want to cast your expression to a boolean:

var foo = Boolean(expression);

or the same thing shortened to double not operators:

var foo = !! expression;

Upvotes: 5

Related Questions