> For the complete documentation index, see [llms.txt](https://huang-jason.gitbook.io/js-syntax/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://huang-jason.gitbook.io/js-syntax/reference-equality-shallow-equality-and-deep-equality.md).

# Reference equality, shallow equality and deep equality

`===`same reference

`shallowEqual` compare each nested object by `===`

`deepEqual` same content, and don't care about reference

`shallowEqual`When it reaches the`address`object, it doesn’t go deeper to compare the contents and relies on the two objects having the same reference.

### refer to the same object

```javascript
user1 = {
    name: "John",
    address: {
        line1: "55 Green Park Road",
        line2: "Purple Valley"  
    }
}

user2 = user1;
console.log("user1 === user2", user1 === user2);
console.log("shallowEqual(user1, user2)", shallowEqual(user1, user2));
console.log("deepEqual(user1, user2)", deepEqual(user1, user2));

// user1 === user2 true // reference equality
// shallowEqual(user1, user2) true
// deepEqual(user1, user2) true
```

### same data but are not referentially equal.

```javascript
user2 = {
    name: "John",
    address: user1.address
}
console.log("user1 === user2", user1 === user2);
console.log("shallowEqual(user1, user2)", shallowEqual(user1, user2));
console.log("deepEqual(user1, user2)", deepEqual(user1, user2));

// user1 === user2 false
// shallowEqual(user1, user2) true
// deepEqual(user1, user2) true
```

### same content

```javascript
user2 = {
    name: "John",
    address: {
        line1: "55 Green Park Road",
        line2: "Purple Valley" 
    }
}
console.log("user1 === user2", user1 === user2);
console.log("shallowEqual(user1, user2)", shallowEqual(user1, user2));
console.log("deepEqual(user1, user2)", deepEqual(user1, user2));

// user1 === user2 false
// shallowEqual(user1, user2) false
// deepEqual(user1, user2) true
```

## equality with JSON.stringify <a href="#checking-for-equality-with-jsonstringify" id="checking-for-equality-with-jsonstringify"></a>

```javascript
function jsonEqual(a,b) {
    return JSON.stringify(a) === JSON.stringify(b);
}
jsonEqual(user1, user2) // true in all three cases above

jsonEqual({a: 1, b: 2}, {b: 2, a: 1}); // false

jsonEqual({a: 5, b: function(){}}, {a: 5}); // true
```

JSON.stringify rely on \[1]the ordering of the properties, \[2]does not serialize functions, \[3]still generate the two complete JSON strings.

deepEqual is much faster at finding out False. As soon as it finds two properties that don’t match up it returns false.

***Prefer deepEqual***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://huang-jason.gitbook.io/js-syntax/reference-equality-shallow-equality-and-deep-equality.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
