0

I just ran into this

var strict = !! argStrict;

I can't help but wonder, what is the meaning of !!? Is it a double negative? Seems pretty redundant, not likely that the guys from php.js would use that in such case?

source: http://phpjs.org/functions/in_array/

php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • https://www.google.com/search?q=javascript%20double%20negation – Yuriy Galanter Jul 30 '14 at 21:27
  • @YuriyGalanter the purpose of this site is to be a better repository of programming knowledge than google. To that end, "just google it" etc is not helpful. – jcollum Jul 30 '14 at 21:28
  • @jcollum the purpose of my link was a message "do a bit of your own research first. SO is not a canned-answer machine". And it is polluted with duplicate question/answers as it is – Yuriy Galanter Jul 30 '14 at 21:29
  • @YuriyGalanter if the question has already been answered on SO then find the duplicate and vote to close. You effectively said "the answer is on google" which misses the point of SO entirely. – jcollum Jul 30 '14 at 21:30
  • @YuriyGalanter Oh but I did try to google it. You need to understand that not all people are good with search engines. – php_nub_qq Jul 30 '14 at 21:30
  • 1
    Good point -- SO is a good place for people to figure out what the terms are. You can't google double negation if you've never heard of double negation. – jcollum Jul 30 '14 at 21:31
  • In fact I did not only try to search for it in google but here as well, and still failed to find any questions. I think I have been gifted with extreme SE-illiteracy – php_nub_qq Jul 30 '14 at 21:32
  • 1
    It's equivalent to `Boolean(argStrict)`, which is probably more readable. – Felix Kling Jul 30 '14 at 21:52

4 Answers4

4

It forces the type to become a true boolean value rather than a "truthy" value. Examples:

var a = (1 === true) // comes out as false because 1 and true are different types and not exactly the same

var b = ((!!1) === true) // comes out as true because the 1 is first converted to a boolean value by means of negation (becomes false) and then negated a second time (becomes true)
Chris Walsh
  • 3,423
  • 2
  • 42
  • 62
1

It means basically "convert to boolean".

It's negating it twice, so it argStrict is "falsy", then !argStrict is true, and !!argStrict is false.

James Curran
  • 101,701
  • 37
  • 181
  • 258
1

It returns a boolean value. false for undefined, null, 0, '' and true any truthy value.

Sarbbottam
  • 5,410
  • 4
  • 30
  • 41
1

This is an example of slacker parsing.

If you have a variable, for example a string, and you want to convert it into a boolean, you could do this:

if (myString) {
    result = true;
}

This says, if myString is NOT undefined, null, empty, the number 0, the boolean false then set my string to the boolean value to true...

But it is faster, and way cooler to double bang it:

result = !! myString;

Other examples include....

//Convert to number
result = +myString;

//Bang Bang Wiggle - coerces an indexOf check into a boolean
result !!~myString.indexOf('e');
Fenton
  • 241,084
  • 71
  • 387
  • 401