JavaScript Interview Questions

23 Questions
JavaScript

JavaScript

Web DevelopmentFrontendBackend

Question 11

What is the difference between '==' and '===' in JavaScript?

Answer:

'==' is the equality operator that performs type coercion before comparing, meaning it converts the operands to the same type before comparison. '===' is the strict equality operator that compares both value and type without converting the operands. Using '===' is generally recommended to avoid unexpected type coercion and ensure that both value and type are considered in the comparison.

Here is an example demonstrating the difference between '==' and '===':

console.log(5 == '5'); // true (type coercion)
console.log(5 === '5'); // false (no type coercion)

console.log(true == 1); // true (type coercion)
console.log(true === 1); // false (no type coercion)

console.log(null == undefined); // true (both are considered equal)
console.log(null === undefined); // false (different types)

In this example, '==' performs type coercion to make the comparisons, while '===' strictly checks both type and value, avoiding the pitfalls of type coercion.

Recent job openings