@jgCarroll/

twoObjects

JavaScript

A common interview question

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// const user1 = {
//   name: 'Jordan',
//   age: 28
// }

// const user2 = {
//   name: 'Jordan',
//   age: 28
// }

// console.log(user1 == user2) // false
// console.log(user1 === user2) // false

// // Two different objects are never equal. We pass objects by reference instead of by value

// // ==================================
// // essentially the same code, but expressed differently

// const user1 = {
//   name: 'Jordan',
//   age: 28
// }

// const user2 = user1

// console.log(user1 == user2) // true
// console.log(user1 === user2) // true

// // Now the two objects evaluate as true because they are referencing the same object, user1

// =======================================
// another way to get them to evaluate as true, turning both objects into JSON strings

const user1 = {
  name: 'Jordan',
  age: 28
}

const user2 = {
  name: 'Jordan',
  age: 28
}

console.log(user1 == user2) // false
console.log(JSON.stringify(user1) === JSON.stringify(user2)) // true
Native Browser JavaScript