javascript: Meaning of two exclamation marks(!!)

Published on:
Last updated:

This post is also available in: 日本語 (Japanese)

I was confused a little when I found two exclamation marks in javascript code.
I make a note so that not forgot.

case: one exclamation mark

var a = true;
console.log(!a); //false
// "booblean" can also converted to a number using "Number".
console.log(Number(!a)); //0

If you want a number in return value rather than booblean(true/false), then use Number(1/0).

case: two exclamation mark

"true" into "not not", then "true" is returned.

var b = true;
console.log(!!b); //true

In the following code, if you use the exclamation mark, you will see that it is converted to booblean(true/false).

var c = undefined; // same if var c =0,null 
console.log(!!c); //false
//Object is true
var d = Object;
console.log(!!d); //true

Reference:What is the !! (not not) operator in JavaScript?

No tags for this post.

About
Kuniyoshi Takemoto is the founder of Amelt.net LLC, and editor of this blog(www.amelt.net).Learn more and follow me on LinkedIn.