Làm thế nào để bạn so sánh các mục trong javascript?

Lưu ý rằng sự khác biệt giữa tất cả những thứ này đều liên quan đến việc xử lý các nguyên mẫu của chúng; . Đối với bất kỳ đối tượng không nguyên thủy nào

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
9 và
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
0 có cấu trúc giống nhau nhưng bản thân chúng là các đối tượng riêng biệt, tất cả các dạng trên sẽ đánh giá là
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
0

Bình đẳng nghiêm ngặt bằng cách sử dụng ===

Bình đẳng nghiêm ngặt so sánh hai giá trị cho bình đẳng. Không có giá trị nào được chuyển đổi hoàn toàn sang một số giá trị khác trước khi được so sánh. Nếu các giá trị có các loại khác nhau, các giá trị được coi là không bằng nhau. Nếu các giá trị có cùng loại, không phải là số và có cùng giá trị thì chúng được coi là bằng nhau. Cuối cùng, nếu cả hai giá trị đều là số, thì chúng được coi là bằng nhau nếu cả hai đều không phải là

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 và có cùng giá trị hoặc nếu một là
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
3 và một là
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2

const num = 0;
const obj = new String("0");
const str = "0";

console.log(num === num); // true
console.log(obj === obj); // true
console.log(str === str); // true

console.log(num === obj); // false
console.log(num === str); // false
console.log(obj === str); // false
console.log(null === undefined); // false
console.log(obj === null); // false
console.log(obj === undefined); // false

Bình đẳng nghiêm ngặt hầu như luôn luôn là hoạt động so sánh chính xác để sử dụng. Đối với tất cả các giá trị ngoại trừ số, nó sử dụng ngữ nghĩa rõ ràng. một giá trị chỉ bằng chính nó. Đối với các số, nó sử dụng ngữ nghĩa hơi khác nhau để đánh bóng trên hai trường hợp cạnh khác nhau. Đầu tiên là số 0 dấu phẩy động được ký dương hoặc âm. Điều này rất hữu ích trong việc biểu diễn một số nghiệm toán học nhất định, nhưng vì hầu hết các tình huống không quan tâm đến sự khác biệt giữa

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
3 và
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2, đẳng thức nghiêm ngặt coi chúng là cùng một giá trị. Thứ hai là dấu phẩy động bao gồm khái niệm về một giá trị không phải là số,
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1, để biểu thị giải pháp cho một số vấn đề toán học không xác định. vô cực âm được thêm vào vô cực dương, ví dụ. Bình đẳng nghiêm ngặt coi
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 là không bình đẳng với mọi giá trị khác — bao gồm cả chính nó. (Trường hợp duy nhất mà
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
9 là
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
20 là khi
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
9 là
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1. )

Bên cạnh

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
63, đẳng thức nghiêm ngặt cũng được sử dụng bởi các phương pháp tìm chỉ mục mảng bao gồm khớp nối
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
24,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
25,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
26,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
27 và
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
28. Điều này có nghĩa là bạn không thể sử dụng
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
29 để tìm chỉ mục của giá trị
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 trong một mảng hoặc sử dụng
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 làm giá trị
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
28 trong câu lệnh
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
43 và làm cho nó khớp với bất kỳ thứ gì

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}

Bình đẳng lỏng lẻo sử dụng ==

Bình đẳng lỏng lẻo là đối xứng.

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
44 luôn có ngữ nghĩa giống hệt với
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
45 đối với bất kỳ giá trị nào của
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
46 và
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
47 (ngoại trừ thứ tự chuyển đổi được áp dụng). Hành vi để thực hiện bình đẳng lỏng lẻo bằng cách sử dụng
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
64 như sau

  1. Nếu các toán hạng có cùng kiểu, chúng được so sánh như sau
    • Sự vật. chỉ trả về
      // Add an immutable NEGATIVE_ZERO property to the Number constructor.
      Object.defineProperty(Number, "NEGATIVE_ZERO", {
        value: -0,
        writable: false,
        configurable: false,
        enumerable: false,
      });
      
      function attemptMutation(v) {
        Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
      }
      
      20 nếu cả hai toán hạng tham chiếu cùng một đối tượng
    • Sợi dây. chỉ trả về
      // Add an immutable NEGATIVE_ZERO property to the Number constructor.
      Object.defineProperty(Number, "NEGATIVE_ZERO", {
        value: -0,
        writable: false,
        configurable: false,
        enumerable: false,
      });
      
      function attemptMutation(v) {
        Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
      }
      
      20 nếu cả hai toán hạng có cùng ký tự theo cùng một thứ tự
    • Con số. chỉ trả về
      // Add an immutable NEGATIVE_ZERO property to the Number constructor.
      Object.defineProperty(Number, "NEGATIVE_ZERO", {
        value: -0,
        writable: false,
        configurable: false,
        enumerable: false,
      });
      
      function attemptMutation(v) {
        Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
      }
      
      20 nếu cả hai toán hạng có cùng giá trị.
      console.log([NaN].indexOf(NaN)); // -1
      switch (NaN) {
        case NaN: console.log("Surprise"); // Nothing is logged
      }
      
      3 và
      console.log([NaN].indexOf(NaN)); // -1
      switch (NaN) {
        case NaN: console.log("Surprise"); // Nothing is logged
      }
      
      2 được coi là cùng một giá trị. Nếu một trong hai toán hạng là
      console.log([NaN].indexOf(NaN)); // -1
      switch (NaN) {
        case NaN: console.log("Surprise"); // Nothing is logged
      }
      
      1, trả về
      const num = 0;
      const big = 0n;
      const str = "0";
      const obj = new String("0");
      
      console.log(num == str); // true
      console.log(big == num); // true
      console.log(str == big); // true
      
      console.log(num == obj); // true
      console.log(big == obj); // true
      console.log(str == obj); // true
      
      0;
    • Boolean. trả lại
      // Add an immutable NEGATIVE_ZERO property to the Number constructor.
      Object.defineProperty(Number, "NEGATIVE_ZERO", {
        value: -0,
        writable: false,
        configurable: false,
        enumerable: false,
      });
      
      function attemptMutation(v) {
        Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
      }
      
      20 chỉ khi toán hạng là cả hai
      // Add an immutable NEGATIVE_ZERO property to the Number constructor.
      Object.defineProperty(Number, "NEGATIVE_ZERO", {
        value: -0,
        writable: false,
        configurable: false,
        enumerable: false,
      });
      
      function attemptMutation(v) {
        Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
      }
      
      20 hoặc cả hai
      const num = 0;
      const big = 0n;
      const str = "0";
      const obj = new String("0");
      
      console.log(num == str); // true
      console.log(big == num); // true
      console.log(str == big); // true
      
      console.log(num == obj); // true
      console.log(big == obj); // true
      console.log(str == obj); // true
      
      0
    • BigInt. chỉ trả về
      // Add an immutable NEGATIVE_ZERO property to the Number constructor.
      Object.defineProperty(Number, "NEGATIVE_ZERO", {
        value: -0,
        writable: false,
        configurable: false,
        enumerable: false,
      });
      
      function attemptMutation(v) {
        Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
      }
      
      20 nếu cả hai toán hạng có cùng giá trị
    • Biểu tượng. chỉ trả về
      // Add an immutable NEGATIVE_ZERO property to the Number constructor.
      Object.defineProperty(Number, "NEGATIVE_ZERO", {
        value: -0,
        writable: false,
        configurable: false,
        enumerable: false,
      });
      
      function attemptMutation(v) {
        Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
      }
      
      20 nếu cả hai toán hạng tham chiếu cùng một ký hiệu
  2. Nếu một trong các toán hạng là
    const num = 0;
    const big = 0n;
    const str = "0";
    const obj = new String("0");
    
    console.log(num == str); // true
    console.log(big == num); // true
    console.log(str == big); // true
    
    console.log(num == obj); // true
    console.log(big == obj); // true
    console.log(str == obj); // true
    
    633 hoặc
    const num = 0;
    const big = 0n;
    const str = "0";
    const obj = new String("0");
    
    console.log(num == str); // true
    console.log(big == num); // true
    console.log(str == big); // true
    
    console.log(num == obj); // true
    console.log(big == obj); // true
    console.log(str == obj); // true
    
    634, thì toán hạng kia cũng phải là
    const num = 0;
    const big = 0n;
    const str = "0";
    const obj = new String("0");
    
    console.log(num == str); // true
    console.log(big == num); // true
    console.log(str == big); // true
    
    console.log(num == obj); // true
    console.log(big == obj); // true
    console.log(str == obj); // true
    
    633 hoặc
    const num = 0;
    const big = 0n;
    const str = "0";
    const obj = new String("0");
    
    console.log(num == str); // true
    console.log(big == num); // true
    console.log(str == big); // true
    
    console.log(num == obj); // true
    console.log(big == obj); // true
    console.log(str == obj); // true
    
    634 để trả về
    // Add an immutable NEGATIVE_ZERO property to the Number constructor.
    Object.defineProperty(Number, "NEGATIVE_ZERO", {
      value: -0,
      writable: false,
      configurable: false,
      enumerable: false,
    });
    
    function attemptMutation(v) {
      Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
    }
    
    20. Nếu không thì trả lại
    const num = 0;
    const big = 0n;
    const str = "0";
    const obj = new String("0");
    
    console.log(num == str); // true
    console.log(big == num); // true
    console.log(str == big); // true
    
    console.log(num == obj); // true
    console.log(big == obj); // true
    console.log(str == obj); // true
    
    0
  3. Nếu một trong các toán hạng là đối tượng và toán hạng kia là nguyên thủy, hãy chuyển đối tượng thành nguyên thủy
  4. Ở bước này, cả hai toán hạng được chuyển đổi thành nguyên hàm (một trong Chuỗi, Số, Boolean, Ký hiệu và BigInt). Phần còn lại của việc chuyển đổi được thực hiện theo từng trường hợp
    • Nếu chúng cùng loại, hãy so sánh chúng bằng bước 1
    • Nếu một trong các toán hạng là Ký hiệu còn toán hạng kia thì không, trả về
      const num = 0;
      const big = 0n;
      const str = "0";
      const obj = new String("0");
      
      console.log(num == str); // true
      console.log(big == num); // true
      console.log(str == big); // true
      
      console.log(num == obj); // true
      console.log(big == obj); // true
      console.log(str == obj); // true
      
      0
    • Nếu một trong các toán hạng là Boolean nhưng toán hạng kia thì không, hãy chuyển đổi boolean thành một số.
      // Add an immutable NEGATIVE_ZERO property to the Number constructor.
      Object.defineProperty(Number, "NEGATIVE_ZERO", {
        value: -0,
        writable: false,
        configurable: false,
        enumerable: false,
      });
      
      function attemptMutation(v) {
        Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
      }
      
      20 được chuyển thành 1 và
      const num = 0;
      const big = 0n;
      const str = "0";
      const obj = new String("0");
      
      console.log(num == str); // true
      console.log(big == num); // true
      console.log(str == big); // true
      
      console.log(num == obj); // true
      console.log(big == obj); // true
      console.log(str == obj); // true
      
      0 được chuyển thành 0. Sau đó so sánh lỏng lẻo hai toán hạng một lần nữa
    • Số thành chuỗi. chuyển chuỗi thành số. Lỗi chuyển đổi dẫn đến
      console.log([NaN].indexOf(NaN)); // -1
      switch (NaN) {
        case NaN: console.log("Surprise"); // Nothing is logged
      }
      
      1, điều này sẽ đảm bảo đẳng thức là
      const num = 0;
      const big = 0n;
      const str = "0";
      const obj = new String("0");
      
      console.log(num == str); // true
      console.log(big == num); // true
      console.log(str == big); // true
      
      console.log(num == obj); // true
      console.log(big == obj); // true
      console.log(str == obj); // true
      
      0
    • Số đến BigInt. so sánh bằng giá trị số của chúng. Nếu số là ±Infinity hoặc
      console.log([NaN].indexOf(NaN)); // -1
      switch (NaN) {
        case NaN: console.log("Surprise"); // Nothing is logged
      }
      
      1, hãy trả về
      const num = 0;
      const big = 0n;
      const str = "0";
      const obj = new String("0");
      
      console.log(num == str); // true
      console.log(big == num); // true
      console.log(str == big); // true
      
      console.log(num == obj); // true
      console.log(big == obj); // true
      console.log(str == obj); // true
      
      0
    • Chuỗi thành BigInt. chuyển đổi chuỗi thành BigInt bằng thuật toán tương tự như hàm tạo
      const num = 0;
      const big = 0n;
      const str = "0";
      const obj = new String("0");
      
      console.log(num == str); // true
      console.log(big == num); // true
      console.log(str == big); // true
      
      console.log(num == obj); // true
      console.log(big == obj); // true
      console.log(str == obj); // true
      
      646. Nếu chuyển đổi không thành công, hãy trả lại
      const num = 0;
      const big = 0n;
      const str = "0";
      const obj = new String("0");
      
      console.log(num == str); // true
      console.log(big == num); // true
      console.log(str == big); // true
      
      console.log(num == obj); // true
      console.log(big == obj); // true
      console.log(str == obj); // true
      
      0

Theo truyền thống và theo ECMAScript, tất cả các đối tượng và đối tượng nguyên thủy đều không bằng nhau đối với

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
634 và
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
633. Nhưng hầu hết các trình duyệt cho phép một lớp đối tượng rất hẹp (cụ thể là đối tượng
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
720 cho bất kỳ trang nào), trong một số ngữ cảnh, hoạt động như thể chúng mô phỏng giá trị
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
634. Bình đẳng lỏng lẻo là một trong những bối cảnh như vậy.
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
722 và
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
723 đánh giá là true nếu và chỉ khi A là một đối tượng mô phỏng
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
634. Trong tất cả các trường hợp khác, một đối tượng không bao giờ lỏng lẻo bằng
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
634 hoặc
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
633

Trong hầu hết các trường hợp, việc sử dụng đẳng thức lỏng lẻo không được khuyến khích. Kết quả so sánh sử dụng đẳng thức nghiêm ngặt sẽ dễ dự đoán hơn và có thể đánh giá nhanh hơn do không có kiểu ép buộc

Ví dụ sau đây minh họa các so sánh đẳng thức lỏng lẻo liên quan đến số nguyên thủy

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727, nguyên hàm bigint
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
728, nguyên hàm chuỗi
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
729 và một đối tượng có giá trị
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
00 là
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
729

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true

Đẳng thức lỏng lẻo chỉ được sử dụng bởi toán tử

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
64

Đẳng thức cùng giá trị sử dụng Object. Là()

Đẳng thức cùng giá trị xác định xem hai giá trị có chức năng giống hệt nhau trong mọi ngữ cảnh hay không. (Trường hợp sử dụng này thể hiện một ví dụ về nguyên tắc thay thế Liskov. ) Một trường hợp xảy ra khi một nỗ lực được thực hiện để thay đổi một thuộc tính không thể thay đổi

// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
03 sẽ đưa ra một ngoại lệ khi cố gắng thay đổi một thuộc tính không thể thay đổi, nhưng sẽ không có tác dụng gì nếu không có thay đổi thực sự nào được yêu cầu. Nếu
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
04 là
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2, không có thay đổi nào được yêu cầu và sẽ không có lỗi xảy ra. Trong nội bộ, khi một thuộc tính bất biến được xác định lại, giá trị mới được chỉ định được so sánh với giá trị hiện tại bằng cách sử dụng đẳng thức cùng giá trị

Đẳng thức cùng giá trị được cung cấp bởi phương pháp

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06. Nó được sử dụng ở hầu hết mọi nơi trong ngôn ngữ mà giá trị nhận dạng tương đương được mong đợi

Bình đẳng giá trị bằng không

Tương tự như đẳng thức cùng giá trị, nhưng +0 và -0 được coi là bằng nhau

Đẳng thức cùng giá trị bằng 0 không được hiển thị dưới dạng API JavaScript, nhưng có thể được triển khai bằng mã tùy chỉnh

// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
2

Same-value-zero chỉ khác với đẳng thức nghiêm ngặt bằng cách coi

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 là tương đương và chỉ khác với đẳng thức cùng giá trị bằng cách coi
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 tương đương với
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727. Điều này làm cho nó thường có hành vi hợp lý nhất trong quá trình tìm kiếm, đặc biệt là khi làm việc với
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1. Nó được sử dụng bởi các phương pháp
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
11,
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
12, cũng như
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
13 và
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
14 để so sánh đẳng thức chính

So sánh các phương pháp đẳng thức

Mọi người thường so sánh số bằng gấp đôi và số bằng gấp ba bằng cách nói cái này là phiên bản "nâng cao" của cái kia. Ví dụ: double equals có thể được coi là phiên bản mở rộng của triple equals, bởi vì cái trước làm mọi thứ mà cái sau làm, nhưng với chuyển đổi kiểu trên toán hạng của nó - ví dụ:

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
15. Ngoài ra, có thể khẳng định rằng dấu bằng kép là cơ sở và dấu bằng ba là phiên bản nâng cao, bởi vì nó yêu cầu hai toán hạng phải cùng loại, do đó, nó bổ sung thêm một ràng buộc

Tuy nhiên, cách suy nghĩ này ngụ ý rằng các so sánh bình đẳng tạo thành một "quang phổ" một chiều trong đó "hoàn toàn nghiêm ngặt" nằm ở một đầu và "hoàn toàn lỏng lẻo" nằm ở đầu kia. Mô hình này không phù hợp với

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06, bởi vì nó không "lỏng lẻo" hơn so với số bằng gấp đôi hoặc "chặt chẽ hơn" so với số bằng gấp ba, cũng như không phù hợp ở đâu đó ở giữa (i. e. , chặt chẽ hơn gấp đôi bằng, nhưng lỏng lẻo hơn gấp ba bằng). Chúng ta có thể thấy từ bảng so sánh sự giống nhau bên dưới rằng điều này là do cách mà
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06 xử lý
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1. Lưu ý rằng nếu
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
19 được đánh giá bằng
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
0, chúng ta có thể nói rằng nó phù hợp với phổ lỏng lẻo/nghiêm ngặt dưới dạng một dạng thậm chí còn chặt chẽ hơn của bộ ba bằng, một dạng phân biệt giữa
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 và
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
3. Việc xử lý
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 có nghĩa là điều này là không đúng sự thật, tuy nhiên. Thật không may,
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06 phải được xem xét theo các đặc điểm cụ thể của nó, thay vì tính lỏng lẻo hoặc nghiêm ngặt của nó đối với các toán tử đẳng thức

xy
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
64
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
63
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
28
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
634
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
634
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
633
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
633
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
20
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
20
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
0
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
0
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
53
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
53
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
3
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
3
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
728
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
84
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
0
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
95
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
0
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
95
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
729
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
31
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
69
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
13
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
14___

Khi nào nên sử dụng đối tượng. is() so với ba bằng

Nói chung, hành vi đặc biệt của

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06 đối với số 0 chỉ có thể được quan tâm khi theo đuổi một số sơ đồ lập trình meta nhất định, đặc biệt là liên quan đến các bộ mô tả thuộc tính, khi công việc của bạn mong muốn phản ánh một số đặc điểm của
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
03. Nếu trường hợp sử dụng của bạn không yêu cầu điều này, bạn nên tránh sử dụng
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06 và thay vào đó sử dụng
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
63. Ngay cả khi các yêu cầu của bạn liên quan đến việc so sánh giữa hai giá trị
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 đánh giá với
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
20, nhìn chung, việc kiểm tra trường hợp đặc biệt của
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 (sử dụng phương pháp
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
92 có sẵn từ các phiên bản trước của ECMAScript) sẽ dễ dàng hơn so với việc tìm ra cách các tính toán xung quanh có thể ảnh hưởng đến

Dưới đây là danh sách chưa đầy đủ các phương thức và toán tử tích hợp có thể gây ra sự khác biệt giữa

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 và
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
3 tự hiển thị trong mã của bạn

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
95 (phủ định đơn nguyên)

Xem xét ví dụ sau

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
4

Nếu

const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
96 là
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727 (hoặc tính thành
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
727), một
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 được đưa vào tại vị trí đó và truyền ra thành
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
00

// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
01,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
02,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
03,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
04

Trong một số trường hợp, có thể đưa

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 vào một biểu thức dưới dạng giá trị trả về của các phương thức này ngay cả khi không tồn tại
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 dưới dạng một trong các tham số. Ví dụ: sử dụng
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
03 để nâng
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
08 lên lũy thừa của bất kỳ số mũ lẻ, âm nào sẽ đánh giá thành
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2. Tham khảo tài liệu cho các phương pháp riêng lẻ

// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
10,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
11,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
12,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
13,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
14,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
15

Có thể nhận được giá trị trả về

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 từ các phương thức này trong một số trường hợp khi tồn tại
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 dưới dạng một trong các tham số. e. g. ,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
18 đánh giá thành
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2. Tham khảo tài liệu cho các phương pháp riêng lẻ

// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
20,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
21,
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
22

Mỗi toán tử này sử dụng thuật toán ToInt32 trong nội bộ. Vì chỉ có một đại diện cho 0 trong loại số nguyên 32 bit bên trong, nên

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 sẽ không tồn tại một chuyến đi khứ hồi sau một phép toán nghịch đảo. e. g. , cả
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
24 và
// Add an immutable NEGATIVE_ZERO property to the Number constructor.
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}
25 đều đánh giá bằng
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true
0

Việc dựa vào

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06 khi không tính đến chữ ký của các số 0 có thể gây nguy hiểm. Tất nhiên, khi mục đích là để phân biệt giữa
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
2 và
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
3, nó sẽ thực hiện chính xác những gì mong muốn

báo trước. Sự vật. là () và NaN

Đặc tả

console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
06 coi tất cả các trường hợp của
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 là cùng một đối tượng. Tuy nhiên, vì có sẵn các mảng đã nhập, nên chúng ta có thể có các biểu diễn dấu chấm động riêng biệt của
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN: console.log("Surprise"); // Nothing is logged
}
1 không hoạt động giống hệt nhau trong mọi ngữ cảnh. Ví dụ

Tôi có thể so sánh hai đối tượng trong JavaScript không?

Mặc dù JavaScript không có giải pháp vượt trội để so sánh hai đối tượng , nhưng không có vấn đề gì khi so sánh hai chuỗi. Do đó, trong phương pháp này, chúng tôi chuyển đổi hai đối tượng của mình thành chuỗi bằng cách sử dụng JSON. stringify() và so sánh hai giá trị để xác định xem các đối tượng có sâu bằng nhau không.

== và === trong JavaScript là gì?

Sự khác biệt chính giữa toán tử == và === trong javascript là toán tử == thực hiện chuyển đổi kiểu của toán hạng trước khi so sánh, trong khi toán tử === . .

Có bao nhiêu cách bạn có thể so sánh hai đối tượng trong JavaScript?

JavaScript cung cấp 3 cách so sánh các giá trị. Toán tử đẳng thức nghiêm ngặt === Toán tử đẳng thức lỏng lẻo == Đối tượng. hàm is().

Là một chức năng để so sánh các đối tượng?

So sánh các đối tượng thật dễ dàng, hãy sử dụng === hoặc Đối tượng. là() . Hàm này trả về true nếu chúng có cùng tham chiếu và false nếu chúng không. Một lần nữa, tôi xin nhấn mạnh, đó là so sánh các tham chiếu đến các đối tượng, không phải các khóa và giá trị của các đối tượng. Vì vậy, từ Ví dụ 3, Đối tượng. là(obj1,obj2); .