Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between double exclamation operator and Boolean() in JavaScript? [duplicate]

Tags:

javascript

I know that !!variable will convert variable into a boolean value and the function Boolean(), according to the ecma262 spec, will also perform a type conversion by calling ToBoolean(value).

My question is: what's the difference? Is !! better in performance than Boolean() ?

like image 638
aztack Avatar asked Feb 26 '26 10:02

aztack


1 Answers

They are the same, as the ! operator will call ToBoolean() internally on its operand, and then flip that returned value, while Boolean() will call ToBoolean() internally on its argument.

like image 162
alex Avatar answered Feb 27 '26 23:02

alex