How is equality checked for primitive values compared to objects in JavaScript?

In JavaScript, equality is checked differently for primitive values compared to objects. The two main concepts to understand are "value equality" and "reference equality."

Primitive Values:
- For primitive values (such as numbers, strings, booleans, etc.), equality is checked based on the value itself.
- When comparing two primitive values using the equality operator (`==` or `===`), JavaScript compares the actual content of the values.

Examples:


let num1 = 5;
let num2 = 5;

console.log(num1 === num2); // Outputs: true (values are equal)

let str1 = "hello";
let str2 = "hello";

console.log(str1 === str2); // Outputs: true (values are equal)


Objects:
- For objects, equality is checked based on reference, not on the content of the object.
- If you compare two objects using the equality operator, it checks whether they reference the exact same object in memory.

Examples:

let obj1 = { key: "value" };
let obj2 = { key: "value" };
let obj3 = obj1;

console.log(obj1 === obj2); // Outputs: false (different references)
console.log(obj1 === obj3); // Outputs: true (same reference)

It's important to note that if you need to check if two objects have the same content, you would typically need to compare their properties individually or use a deep equality check. Libraries like Lodash provide functions for deep equality checks (`_.isEqual()`), or you can implement your own custom comparison logic based on your specific requirements.