admin管理员组

文章数量:1334373

I expected !!"false" to return false i.e. !"false" would return true, so !!"false" would return "false", but when I tested it in the console, !!"false" returned true.

Why didn`t things happen as expected?

I expected !!"false" to return false i.e. !"false" would return true, so !!"false" would return "false", but when I tested it in the console, !!"false" returned true.

Why didn`t things happen as expected?

Share Improve this question edited Mar 18, 2011 at 4:57 mjmitche asked Mar 18, 2011 at 4:39 mjmitchemjmitche 2,0676 gold badges25 silver badges31 bronze badges 1
  • Possible duplicate of How can I convert a string to boolean in JavaScript?. Voted to close. – MAK Commented Mar 18, 2011 at 4:42
Add a ment  | 

2 Answers 2

Reset to default 12

"false" is a non-empty string, which evaluates to true. Hence !"false" is false and !!"false" is true. You were probably thinking of !!false.

The reason this is occurring because anything other than an empty string will return true.

本文标签: JavaScript quotfalsequotStack Overflow