Home >Web Front-end >JS Tutorial >Why == s true but {} === {} is false.
JavaScript’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
.
To understand this behavior, you need to understand how JavaScript handles raw values and objects in memory.
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
.
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
.
The following is a simplified diagram:
<code class="language-javascript">const obj1 = {}; // 在内存位置 A 创建 const obj2 = {}; // 在内存位置 B 创建 console.log(obj1 === obj2); // false,因为 A !== B const obj3 = obj1; // obj3 指向与 obj1 相同的位置 console.log(obj1 === obj3); // true,因为两者都指向 A</code>
This difference is because the objects are reference types, which means that the comparison checks whether the references are identical, not the contents.
typeof
to check value typeThe typeof
operator in JavaScript helps you understand the type of a value. Here's how to use it:
<code class="language-javascript">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"</code>
null
and arrays are objectsYou 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 indeed objects, but they have one unique property: [[Call]]
. This internal property makes them callable, which is why you can call them like this:
<code class="language-javascript">function greet() { console.log("Hello!"); } greet(); // "Hello!"</code>
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:
<code class="language-javascript">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</code>
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!