Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Considering best practices, can we use double unary operator ! in JavaScript?

Tags:

javascript

For my understanding the unary ! operator performs implicit type conversions, and are sometimes used for type conversion.

So basically the ! operator converts its operand to a boolean and negates it.

Now:

!!x // Same as Boolean(x)

In fact:

!!'true' === Boolean('true') // true

So I am assuming both !!x and Boolean(x) perform the same action.

I would like to know:

  • Do you know any caveats making my assumptions wrong?
  • Which way should be preferred in term of good practice?
  • Do you know any differences to be aware among different ECMAScript versions or Browser vendors?
like image 612
GibboK Avatar asked Jul 01 '15 07:07

GibboK


People also ask

What is unary plus in JavaScript?

Unary plus (+) The unary plus operator ( + ) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.

Which of the following is unary operator?

The unary operators are as follows: Indirection operator ( * ) Address-of operator ( & ) Unary plus operator ( + )

What does the unary operator typeof returns if the operand value is null in JavaScript?

Answer: The “unary operator” type of returns the object if the operand value is Null. EXPLANATION: In the expression a+b=c, a and b are operators, + and = are operators and c is the result.


2 Answers

Do you know any caveats making my assumptions wrong?

Your assumption is correct. Both Boolean function and !! has the same functionality.

Which way should be preferred in term of good practice?

I recommend using unary operator twice because it is atleast two times faster than using Boolean with 100,000 iterations

JS fiddle link: https://jsfiddle.net/vj593auw/1/

Do you know any differences to be aware among different ECMAScript versions or Browser vendors?

Boolean and unary operators are implemented from JavaScript 1.0, so they should be available in all browsers that supports JavaScript.

like image 195
Kira Avatar answered Oct 16 '22 13:10

Kira


  • Your assumptions are correct. That is exactly how it works and I'm not aware of any special care to be taken when using this.

  • Speaking of good practice, you'll probably have as many people supporting the quick 'n "dirty" !! way as there are who would advocate using the Boolean function ; however, from my experience, it appears that !! is way more common in library code (look at the jQuery source, you have a lot of stuff like return !!locked;). IMHO, it is sufficiently recognizable to be used without degrading code readability.

  • This has all been standard for a very long time in ECMAScript ; I can't speak for old versions of Internet Explorer (before IE 8), but you can pretty much trust all modern browsers to behave the same in this case.

Useful references in the standard :

  • http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.9
  • http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
like image 44
ttzn Avatar answered Oct 16 '22 14:10

ttzn