Every value has truth or false values in JavaScript. For example, a null
value has an associated boolean value of false. Similarly 34
has an associated value of true. We can use this to cast a variable to true or false using the double bang operator.
Let’s dive deep into what it is and how it works.
The !
in JavaScript, also called “bang”, is the logical “not” operator. If you place this operator in front of a boolean value, it will reverse the value, returning the opposite.
!true // returns false
!false // returns true
isTrue = true // variable which is boolean
!isTrue // returns false
If the single bang returns the opposite boolean value, imagine what double-bang would return?
The associated boolean value. In other words, true or false according to whether it is truthy or falsy values.
Values that are associated with boolean true are said to be truthy. Values that are associated with boolean false values are said to be falsy.
!!true // returns true
!!false // returns false
isTrue = true // variable which is boolean
!!isTrue // returns true
We can take advantage of this using double-bang on non-boolean values as well which is pretty cool.
isNumber = 34 // variable which is not boolean
!!isNumber // returns true
Truthy values:
In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context.
The following values are few examples that are considered by JavaScript to be truthys:
- Object:
{}
- Array:
[]
- Not empty string:
"anything"
- Number other than zero:
3.14
- Date:
new Date();
In the below example, variable something
has the non-empty string value which has truthy value in JavaScript so the console will print the first message.
var something = ‘string’;
if (!!something) {
console.log('This is truthy')
} else {
console.log('This is falsey')
}
You can find more about it in the link here.
Falsy values:
A falsy value is a value that is considered false when encountered in a Boolean context.
The following values are few of the examples that are considered by JavaScript to be falsey:
- Empty string:
""
0
null
undefined
-
NaN
and the list of falsy values below.
In the below example, variable nothing
has the 0
which has falsy value in JavaScript so the console will print the second message.
var nothing = 0;
if (!!nothing) {
console.log('This is truthy')
} else {
console.log('This is falsey')
}
You can find more about falsy values in the link here.
Let us see how we can use it for type casting.
function BankAccount(cash) {
this.cash = cash;
this.hasMoney = !!cash;
}
var myAccount = new BankAccount(80);
console.log(myAccount.cash); // expected result: 80
console.log(myAccount.hasMoney); // expected result: true
var emptyAccount = new BankAccount(0);
console.log(emptyAccount.cash); // expected result: 0
console.log(emptyAccount.hasMoney); // expected result: false
And that sums it up!
Thank You!
Top comments (14)
This is very well explained. You can also call this the double NOT operator if you'd like.
While
Boolean(someValue)
the same things as!!someValue
, using the double not operator is shorter to write and a tiny bit faster. The speed increase is nothing meaningful, but interesting nonetheless.Benchmark I ran in case you want to see for yourself:
This is interesting. Never thought about
Boolean()
. Thank you.It isn't a double bang operator, it is using the ! operator twice. Also, since - as you rightly state - values already have truthy and falsey-ness, it is largely pointless to use this unless you really, really need a pure boolean (perhaps for building some JSON to send somewhere on another system that requires the correct type)
That rocks, I remember it as "Bang Bang, you're a boolean now".
Bang Bang, you're a Boolean now
I totally had forgotten about this. :)
I feel like they article made this whole concept really complicated. It's actually simple, What is
!!
?. It's the same asCoerce to boolean
. It would do the same as a methodConvert.toBoolean(value)
where value istruthy
orfalsy
and the result istrue
orfalse
.You need this because some parameters are expected to be
true
orfalse
and nottruthy
andfalsy
.Boolean(value)
doesn't actually coerce a value to a boolean, it's unfortunately less useful than that. And so using!!
(the double NOT) is a great way to coerce a value to a boolean.Cool!! I didn't know 👍
👍
what i learned that if a value is true or false.
by default it does not return anything if it is not explicitly set with true or false boolean . that means by default it returns the value only.
if single bang (!) is added before the value, it returns false.
if double bang (!!) is added before the value, it returns true.
am i correct ?
@geometry dash lite Instead of using a double bang operator, it is twice using the! operator. As you properly point out, values already have truthiness and falseness, so using this is practically pointless unless you really, truly need a pure boolean (maybe for making some JSON to transmit someplace on another system that has the correct type)
Good stuff!
❤️ clear 👍👍
Hey Sanchithasr! I'm stuck in a problem in React JS can you please help asap
Hey. I have not worked on React much. Please post the problem. Someone might be able to help out. :)