JavaScript で、null が 0 に等しいかどうかについての議論
友達が null が 0 に等しいかどうかを質問して議論しているのを見ました。
これを聞いたら、デモを書いて試してみてください。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> <script type="text/javascript"> console.log(null > 0); // false console.log(null < 0); // false console.log(null >= 0); // true console.log(null <= 0); // true console.log(null == 0); // false console.log(null === 0); // false </script> </html>
console.log(null <= 0); と console.log(null >= 0); の 2 つの判定はなぜ true なのでしょうか?
まず、ES3 の内部等価演算のアルゴリズム実装を見てみましょう。
11.9.3 The Abstract Equality Comparison Algorithm The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows: 1. If Type(x) is different from Type(y), Go to step 14. 2. If Type(x) is Undefined, return true. 3. If Type(x) is Null, return true. 4. If Type(x) is not Number, go to step 11. 5. If x is NaN, return false. 6. If y is NaN, return false. 7. If x is the same number value as y, return true. 8. If x is +0 and y is -0, return true. 9. If x is -0 and y is +0, return true. 10. Return false. 11. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false. 12. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false. 13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false. 14. If x is null and y is undefined, return true. 15. If x is undefined and y is null, return true. 16. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y). 17. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x)== y. 18. If Type(x) is Boolean, return the result of the comparison ToNumber(x)== y. 19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y). 20. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y). 21. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x)== y. 22. Return false.
次に、ES3 の内部リレーショナル操作のアルゴリズム実装を見てみましょう。
11.8.5 The Abstract Relational Comparison Algorithm The comparison x < y, where x and y are values, produces true, false, or undefined (which indicates that at least one operand is NaN). Such a comparison is performed as follows: 1. Call ToPrimitive(x, hint Number). 2. Call ToPrimitive(y, hint Number). 3. If Type(Result(1)) is String and Type(Result(2)) is String, go to step 16. (Note that this step differs from step 7 in the algorithm for the addition operator **+ * in using *and instead of or.) 4. Call ToNumber(Result(1)). 5. Call ToNumber(Result(2)). 6. If Result(4) is NaN, return undefined. 7. If Result(5) is NaN, return undefined. 8. If Result(4) and Result(5) are the same number value, return false. 9. If Result(4) is +0 and Result(5) is -0, return false. 10. If Result(4) is -0 and Result(5) is +0, return false. 11. If Result(4) is +∞, return false. 12. If Result(5) is +∞, return true. 13. If Result(5) is -∞, return false. 14. If Result(4) is -∞, return true. 15. If the mathematical value of Result(4) is less than the mathematical value of Result(5) — note that these mathematical values are both finite and not both zero — return true. Otherwise, return false. 16. If Result(2) is a prefix of Result(1), return false. (A string value p is a prefix of string value q if q can be the result of concatenating p and some other string*r*. Note that any string is a prefix of itself, because r may be the empty string.) 17. If Result(1) is a prefix of Result(2), return true. 18. Let k be the smallest nonnegative integer such that the character at position k within Result(1) is different from the character at position k within Result(2). (There must be such a k, for neither string is a prefix of the other.) 19. Let m be the integer that is the code point value for the character at position k within Result(1). 20. Let n be the integer that is the code point value for the character at position k within Result(2). 21. If m < n, return true. Otherwise, return false.
ES3の「>」演算子:
The Greater-than Operator ( > ) The production RelationalExpression : RelationalExpression > ShiftExpression is evaluated as follows: 1. Evaluate RelationalExpression. 2. Call GetValue(Result(1)). 3. Evaluate ShiftExpression. 4. Call GetValue(Result(3)). 5. Perform the comparison Result(4) < Result(2). 6. If Result(5) is undefined, return false. Otherwise, return Result(5).
ES3の「>=」演算子:
The Greater-than-or-equal Operator ( >= ) The production RelationalExpression : RelationalExpression >= ShiftExpression is evaluated as follows: 1. Evaluate RelationalExpression. 2. Call GetValue(Result(1)). 3. Evaluate ShiftExpression. 4. Call GetValue(Result(3)). 5. Perform the comparison Result(2) < Result(4). (see 11.8.5). 6. If Result(5) is true or undefined, return false. Otherwise, return true.
ES3の「==」演算子:
The Equals Operator ( == ) The production EqualityExpression : EqualityExpression == RelationalExpression is evaluated as follows: 1. Evaluate EqualityExpression. 2. Call GetValue(Result(1)). 3. Evaluate RelationalExpression. 4. Call GetValue(Result(3)). 5. Perform the comparison Result(4) == Result(2). (see 11.9.3). 6. Return Result(5).
データに基づく内容
- al 演算子と等価性
- 関係演算子は、設計上、常にオペランドを数値に変換する必要があります。設計上、等価演算子はこれを考慮しません。 a > b および a == b は当然のことであり、a >= b の関係が確立されます。a > b および a >= b はグループです。 b および他の等価演算子はグループです。たとえば、 a === b 、 a != b 、 a !== b 。
- この問題を逆に見ることができます。
null > 0 // null 尝试转型为number , 则为0 . 所以结果为 false, null >= 0 // null 尝试转为number ,则为0 , 结果为 true. null == 0 // null在设计上,在此处不尝试转型. 所以 结果为false.
ログイン後にコピーa >= b 演算子は、a < b の結果を単純に反転します。これが設計上の誤りであると考えるもう 1 つの理由は、未定義の場合に注意してください。設計、未定義 > 、未定義 < 0、未定義 == 0 の結果は設計とロジックと一致しています。そして、null は、今朝になって初めて、関連する章を読み直しました。 ES3 と 5。この問題を根本的に理解していないことに突然気づきました。
別の例
function case1(a){ if(a == null){ .... } } function case2(a){ if(a == undefined){ ... } } // 上面两组完全等价, 这就是一种不明确表述. // 我们永远不知道代码编写者的目的到底是同时匹配null 和 undefined还是只匹配其中某一个 function case3(a){ if(a === null || a === undefined){ ... } } // case3 才是最好的表述. 我们明确知道代码编写者的意图. // 即使很多人可能认为这个代码很愚蠢. 但我坚定的认为这才是好代码.
以上がJavaScript で、null が 0 に等しいかどうかについての議論の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











WebSocket と JavaScript を使用してオンライン音声認識システムを実装する方法 はじめに: 技術の継続的な発展により、音声認識技術は人工知能の分野の重要な部分になりました。 WebSocket と JavaScript をベースとしたオンライン音声認識システムは、低遅延、リアルタイム、クロスプラットフォームという特徴があり、広く使用されるソリューションとなっています。この記事では、WebSocket と JavaScript を使用してオンライン音声認識システムを実装する方法を紹介します。

WebSocketとJavaScript:リアルタイム監視システムを実現するためのキーテクノロジー はじめに: インターネット技術の急速な発展に伴い、リアルタイム監視システムは様々な分野で広く利用されています。リアルタイム監視を実現するための重要なテクノロジーの 1 つは、WebSocket と JavaScript の組み合わせです。この記事では、リアルタイム監視システムにおける WebSocket と JavaScript のアプリケーションを紹介し、コード例を示し、その実装原理を詳しく説明します。 1.WebSocketテクノロジー

WebSocket と JavaScript を使用してオンライン予約システムを実装する方法 今日のデジタル時代では、ますます多くの企業やサービスがオンライン予約機能を提供する必要があります。効率的かつリアルタイムのオンライン予約システムを実装することが重要です。この記事では、WebSocket と JavaScript を使用してオンライン予約システムを実装する方法と、具体的なコード例を紹介します。 1. WebSocket とは何ですか? WebSocket は、単一の TCP 接続における全二重方式です。

JavaScript と WebSocket を使用してリアルタイム オンライン注文システムを実装する方法の紹介: インターネットの普及とテクノロジーの進歩に伴い、ますます多くのレストランがオンライン注文サービスを提供し始めています。リアルタイムのオンライン注文システムを実装するには、JavaScript と WebSocket テクノロジを使用できます。 WebSocket は、TCP プロトコルをベースとした全二重通信プロトコルで、クライアントとサーバー間のリアルタイム双方向通信を実現します。リアルタイムオンラインオーダーシステムにおいて、ユーザーが料理を選択して注文するとき

JavaScript チュートリアル: HTTP ステータス コードを取得する方法、特定のコード例が必要です 序文: Web 開発では、サーバーとのデータ対話が頻繁に発生します。サーバーと通信するとき、多くの場合、返された HTTP ステータス コードを取得して操作が成功したかどうかを判断し、さまざまなステータス コードに基づいて対応する処理を実行する必要があります。この記事では、JavaScript を使用して HTTP ステータス コードを取得する方法を説明し、いくつかの実用的なコード例を示します。 XMLHttpRequestの使用

JavaScript と WebSocket: 効率的なリアルタイム天気予報システムの構築 はじめに: 今日、天気予報の精度は日常生活と意思決定にとって非常に重要です。テクノロジーの発展に伴い、リアルタイムで気象データを取得することで、より正確で信頼性の高い天気予報を提供できるようになりました。この記事では、JavaScript と WebSocket テクノロジを使用して効率的なリアルタイム天気予報システムを構築する方法を学びます。この記事では、具体的なコード例を通じて実装プロセスを説明します。私たちは

JavaScript で HTTP ステータス コードを取得する方法の紹介: フロントエンド開発では、バックエンド インターフェイスとの対話を処理する必要があることが多く、HTTP ステータス コードはその非常に重要な部分です。 HTTP ステータス コードを理解して取得すると、インターフェイスから返されたデータをより適切に処理できるようになります。この記事では、JavaScript を使用して HTTP ステータス コードを取得する方法と、具体的なコード例を紹介します。 1. HTTP ステータス コードとは何ですか? HTTP ステータス コードとは、ブラウザがサーバーへのリクエストを開始したときに、サービスが

使用法: JavaScript では、insertBefore() メソッドを使用して、DOM ツリーに新しいノードを挿入します。このメソッドには、挿入される新しいノードと参照ノード (つまり、新しいノードが挿入されるノード) の 2 つのパラメータが必要です。
