This content originally appeared on DEV Community and was authored by Sooraj S
What's the output ?
function checkAge(data) {
if (data === { age: 18 }) {
console.log('You are an adult!');
} else if (data == { age: 18 }) {
console.log('You are still an adult.');
} else {
console.log(`Hmm.. You don't have an age I guess`);
}
}
checkAge({ age: 18 });
- A:
You are an adult!
- B:
You are still an adult.
- C:
Hmm.. You don't have an age I guess
Answer: C
When testing equality, primitives are compared by their value, while objects are compared by their reference. JavaScript checks if the objects have a reference to the same location in memory.
The two objects that we are comparing don't have that: the object we passed as a parameter refers to a different location in memory than the object we used in order to check equality.
This is why both { age: 18 } === { age: 18 }
and { age: 18 } == { age: 18 }
return false
.
This content originally appeared on DEV Community and was authored by Sooraj S
Sooraj S | Sciencx (2021-07-18T12:52:36+00:00) JavaScript question #Day 8. Retrieved from https://www.scien.cx/2021/07/18/javascript-question-day-8/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.