JS Coding Questions Logo
JS Coding Questions
#70💼 Interview💻 Code

What is the purpose of double exclamation

Advertisement

728x90

The double exclamation or negation(!!) ensures the resulting type is a boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, it will be true.

For example, you can test IE version using this expression as below,

javascript
1let isIE8 = false;
2
3isIE8 = !!navigator.userAgent.match(/MSIE 8.0/);
4
5console.log(isIE8); // returns true or false

If you don't use this expression then it returns the original value.

javascript
1console.log(navigator.userAgent.match(/MSIE 8.0/)); // returns either an Array or null

Note: The expression !! is not an operator, but it is just twice of ! operator.

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 71

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
70of476