Table of Contents
Memory mechanism: primitive values ​​and objects
Original value
Object
Deep dive: memory and heap
Use typeof to check value type
Unexpected: null and arrays are objects
Functions are special objects
Importance
Conclusion
Home Web Front-end JS Tutorial Why == s true but {} === {} is false.

Why == s true but {} === {} is false.

Jan 19, 2025 am 08:33 AM

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.

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
Copy after login

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"
Copy after login

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!"
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot tools Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

jQuery Check if Date is Valid

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

jQuery get element padding/margin

Top 5 Date Manipulation JS Plugins Top 5 Date Manipulation JS Plugins Feb 28, 2025 am 12:34 AM

Top 5 Date Manipulation JS Plugins

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

10 Worth Checking Out jQuery Plugins

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

10 jQuery Accordions Tabs

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

jquery add scrollbar to div

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

See all articles