Why == s true but {} === {} is false.
Jan 19, 2025 am 08:33 AMJavaScript’s strict equality operator (===) seems simple at first glance, but some situations may confuse you. Let's explore why 1 === 1
returns true
, while {} === {}
returns false
.
Memory mechanism: primitive values and objects
To understand this behavior, you need to understand how JavaScript handles raw values and objects in memory.
Original value
Primitive values include numbers, strings, booleans, undefined
, null
and symbols. When comparing two primitive values using ===
, JavaScript checks whether their values are the same. Since the two 1 === 1
in 1
refer to the same value, the comparison results in true
.
Object
Objects are more complex. They are stored in a special area of memory called the Heap. Every time an object is created, it gets a new memory location in the heap. When you use ===
to compare objects, JavaScript checks whether the two objects reference the same memory location. Since {}
and {}
are two different objects created in memory and they do not share the same memory location, the result of {} === {}
is false
.
Deep dive: memory and heap
The following is a simplified diagram:
const obj1 = {}; // 在内存位置 A 创建 const obj2 = {}; // 在内存位置 B 创建 console.log(obj1 === obj2); // false,因为 A !== B const obj3 = obj1; // obj3 指向与 obj1 相同的位置 console.log(obj1 === obj3); // true,因为两者都指向 A
This difference is because the objects are reference types, which means that the comparison checks whether the references are identical, not the contents.
Use typeof
to check value type
The typeof
operator in JavaScript helps you understand the type of a value. Here's how to use it:
console.log(typeof "hello"); // "string" console.log(typeof 42); // "number" console.log(typeof true); // "boolean" console.log(typeof undefined);// "undefined" console.log(typeof null); // "object" (已知的 bug!) console.log(typeof {}); // "object" console.log(typeof []); // "object" console.log(typeof function () {}); // "function"
Unexpected: null
and arrays are objects
You may notice something strange: typeof null
Returns "object". This is a historical bug in JavaScript that cannot be fixed without breaking the web. However, arrays and functions are indeed objects under the hood.
Functions are special objects
Functions are indeed objects, but they have one unique property: [[Call]]
. This internal property makes them callable, which is why you can call them like this:
function greet() { console.log("Hello!"); } greet(); // "Hello!"
Importance
Understanding the difference between primitive values and objects is critical to writing robust JavaScript code. When comparing objects, use deep equality checks if you want to compare their contents. For example:
const objA = { name: "Alice" }; const objB = { name: "Alice" }; // 浅比较 console.log(objA === objB); // false // 深度比较 const isEqual = JSON.stringify(objA) === JSON.stringify(objB); console.log(isEqual); // true
Conclusion
The key is that 1 === 1
returns true
because primitives compare by value, and {} === {}
returns false
because objects compare by reference. Once you master this, you'll be able to better understand the features and intricacies of JavaScript!
The above is the detailed content of Why == s true but {} === {} is false.. For more information, please follow other related articles on the PHP Chinese website!

Hot tools Tags

Hot Article

Hot tools Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial
