0
  • I am trying to understand a small piece of code
  • I thought !!in javascript does the not operator two times

    var result = !!String("false");
    
  • so I thought for the above code it will return as false

  • but it returns true..and can you tell me why the type is boolean?
3
  • This was closed as a duplicate of stackoverflow.com/questions/784929/… but I reopened it. The question isn't about what !! does, but why it returns a different result than he expected with this particular argument. Commented Mar 3, 2016 at 17:49
  • The really simple explanation, is that the string "false" is in fact true Commented Mar 3, 2016 at 17:50
  • Plus the fact that ! always converts its argument to a boolean, so !!<anything> is not the same as <anything> unless <anything> is already a boolean. Commented Mar 3, 2016 at 17:52

2 Answers 2

5

That is because,

  1. String("false") will return "false". A string.

  2. !"false" will be evaluated to false. Since a non empty string will be considered as true when it coerces to boolean.

  3. !false will be evaluated to true.

Sign up to request clarification or add additional context in comments.

Comments

2
String("false")

Resolves to:

"false"

Which is a string that is non-empty, so

!"false"

Resolves to

false

And finally

!false

Resolves to

true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.