WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

JavaScript Β· Basic Β· question 5 of 100

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

πŸ“• Buy this interview preparation book: 100 JavaScript questions & answers β€” PDF + EPUB for $5

In JavaScript, the "==" and "===" operators are used to compare values. However, they have different behavior and should be used in different contexts.

The "==" operator is called the equality operator and is used to compare two values for equality. It performs type coercion, meaning that it converts the values to a common type before comparing them. This can sometimes lead to unexpected results, as different types can be considered equal in some cases.

Example:

    console.log(10 == '10'); // Outputs true

In this example, the number 10 and the string ’10’ are considered equal because the "==" operator performs type coercion and converts the string to a number before comparing them.

The "===" operator is called the strict equality operator and is used to compare two values for equality without performing type coercion. It compares both the value and the type of the operands, and only returns true if both are identical.

Example:

    console.log(10 === '10'); // Outputs false

In this example, the "===" operator returns false because the two operands have different types, and strict equality requires both the value and the type to be the same.

In general, it is recommended to use the "===" operator whenever possible, as it ensures that the comparison is strict and does not rely on type coercion. The "==" operator should be used only when type coercion is intended or when comparing values of different types.

It is also worth noting that there is a corresponding set of "!=" and "!==" operators, which are used to test for inequality. These operators have the same behavior as their "==" and "===" counterparts, but return true if the operands are not equal.

Example:

    console.log(10 != '10'); // Outputs false
    console.log(10 !== '10'); // Outputs true

In summary, the "==" and "===" operators are used to compare values in JavaScript, with the former performing type coercion and the latter not. As a JavaScript expert, I would be happy to provide more information or examples if needed.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview β€” then scores it.
πŸ“ž Practice JavaScript β€” free 15 min
πŸ“• Buy this interview preparation book: 100 JavaScript questions & answers β€” PDF + EPUB for $5

All 100 JavaScript questions Β· All topics