shallow comparison

Solutions on MaxInterview for shallow comparison by the best coders in the world

showing results for - "shallow comparison"
Carey
04 Nov 2019
1When comparing scalar values (numbers, strings) it compares their values. 
2When comparing objects, it does not compare their attributes
3- only their references are compared
4Shallow compare is an efficient way to detect changes
5// Example:
6user = {
7  name: "John",
8  surname: "Doe"
9}
10
11// User1
12const user = this.state.user;
13user.name = "Jane";
14
15console.log(user === this.state.user); // true:The references are the same.
16
17// User2
18const user = clone(this.state.user);
19console.log(user === this.state.user); 
20// false:Cloning: you create a new copy with a different reference