Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ' !!window.google ' mean? [duplicate]

enter image description here

I'm reading through https://markus.oberlehner.net/blog/using-the-google-maps-api-with-vue/ . In the section in the screenshot the author uses:

let initialized = !!window.google;

What does this mean?

like image 276
user1592380 Avatar asked Apr 22 '26 07:04

user1592380


1 Answers

window.google either evaluates to something, or it evaluates to undefined.

Before we get to your question, we need to look at the concept of "truthiness"

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN

Okay, back to your question. Let us assume it is something (a truthy value). !something will evaluate to false. !false evaluates to true, so !!something will evaluate to true.

Assume now that it is undefined. In this case !undefined evaluates to true, then !true evaluates to false. So, !!undefined will evaluate to false.

In other words: if it is something (truthy), it is initialized. If not, then it hasn't been initialized.

So, ! before a truthy value (something) will make it false, then the extra ! negates that. Truthy things become true, and falsy things become false.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!