• Post author:
  • Post category:Blog
  • Reading time:2 mins read
  • Post last modified:June 12, 2024

Which operator do we use if we want to check if two variables store the same values of exactly the same type?

  • =
  • ===
  • ==
  • !==
Answers Explanation & Hints:

To check if two variables store the same values of exactly the same type in JavaScript, you would use the strict equality operator, which is “===”.

Here’s an explanation of the different comparison operators you mentioned:

  • “===” (strict equality operator): It compares two values for equality without performing any type coercion. It returns true if the values are equal in value and type.
  • “==” (loose equality operator): It also compares two values for equality, but it allows for type coercion. JavaScript will try to convert the operands to the same type before making the comparison. This operator can lead to unexpected results due to the type coercion rules, so it’s generally recommended to use the strict equality operator (===) instead.
  • “!=” (loose inequality operator): It checks if two values are not equal after performing type coercion.
  • “!==” (strict inequality operator): It checks if two values are not equal without performing any type coercion.

For comparing variables to check if they hold the same values of the same type, you should use the strict equality operator (===). For example:

const variable1 = 5;
const variable2 = 5;

console.log(variable1 === variable2); // Output: true

In the above example, both variable1 and variable2 hold the same value of 5, and since the strict equality operator is used, the comparison evaluates to true.

For more Questions and Answers:

JavaScipt Essentials 1 (JSE) | JSE1 – Module 3 Test Exam Answers Full 100% 

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments