!演算子は常にまたはを返すためtrue、false評価がx正常に完了すると仮定する!!xと、ブール値はEcmaScript組み込み関数のBoolean(x)whereと同等です。Boolean
http://es5.github.com/#x11.4.9によると
11.4.9論理否定演算子(!)
プロダクションUnaryExpression:! UnaryExpressionは次のように評価されます。
- ExprをUnaryExpressionの評価結果とします。
- oldValueをToBoolean(GetValue(expr))とします。
- oldValueが
true、の場合は、を返しfalseます。
- 戻り
trueます。
http://es5.github.com/#x9.2は、 ToBooleanの仕組みを説明しています
9.2 ToBoolean
抽象演算ToBooleanは、その引数を表11に従ってブール型の値に変換します。
表11—ToBoolean変換
Argument Type Result
Undefined false
Null false
Boolean The result equals the input argument (no conversion).
Number The result is false if the argument is +0, −0, or NaN; otherwise the result is true.
String The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
Object true
上記の表はほとんどの例を説明していますが、理由が明確でない場合があります
console.log(boolCheck());
ですfalse。仮パラメーターよりも実際の引数が少ない関数を呼び出す場合、追加のパラメーターには値がundefinedあり、上の表に示すように、は!!undefinedですfalse。
やる上での落とし穴はありますか:!!someValue?
意図はより明確ではありませんがBoolean(someValue)、プラットフォーム間で一貫して機能し、ほとんどの経験豊富なJavascript開発者がそれを認識します。