2

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?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • In this case wouldn't it just be checking if there's a `google` value saved on the `window` object and converting it to a boolean using the double `!!`? – Carl Edwards May 24 '19 at 18:00

3 Answers3

8

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.

  • whats the difference between `if (!!something)` and `if (something)` – Isaac Vidrine May 24 '19 at 18:06
  • @IsaacVidrine Generally nothing. You only use double negative if you want to *return* that value or use it somewhere else. For logical comparisons it's pointless. Note in the question this is being assigned to a variable. `!!` means "I don't care what the value is, just if it's logically true or false". – tadman May 24 '19 at 18:08
  • @tadman makes sense, thanks! – Isaac Vidrine May 24 '19 at 18:08
  • 1
    I think of it as "flattening" a truthy/falsy value to true or false. –  May 24 '19 at 18:08
  • @IsaacVidrine That would throw that error. Member access has a higher [order of precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence) than logical not. –  May 24 '19 at 18:15
  • @Amy yup haha just tested it and realized that was not the case. – Isaac Vidrine May 24 '19 at 18:15
3

!! is a shorthand way of converting a variable into Boolean value. If google script is loaded correctly, window.google would exist as an object. !object would return false (if it is legit value, it is converted to true in Boolean checks in js, object is evaluated as true, !true is false). Second not operating converts the false back to true, so object is converted into a Boolean value.

Same way, if there was error loading the script, then window.google would be 'undefined', which is evaluated to be false. using double not operating you convert undefined into actual Boolean value false.

Kanwal Sarwara
  • 403
  • 4
  • 15
1

!!expression is a terse idiom to cast an expression's value to a Boolean.

In this case, initialized will be set to true or false based on whether there's a global google property with a truthy value.

Ates Goral
  • 137,716
  • 26
  • 137
  • 190