Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


Which of the following is true regarding primitive value comparison in JavaScript?

  1. They are compared by reference.

  2. They can be checked for equality using == and ===.

  3. They must be of the same type to compare.

  4. They return false when two objects are compared.

The correct answer is: They can be checked for equality using == and ===.

In JavaScript, primitive values are compared based on their actual value rather than their reference in memory. This means that for primitive types such as numbers, strings, booleans, null, undefined, and symbols, JavaScript allows for comparison using both the equality operator (==) and the strict equality operator (===). The equality operator (==) checks for value equality with type coercion if the types do not match, while the strict equality operator (===) checks for both value and type equality, ensuring that both operands are of the same type and value before considering them equal. This dual capability makes option B accurate, as it identifies that primitive values can indeed be checked for equality using both of these operators. Other options may confuse the nature of primitive comparison: they either misconstrue how primitives are identified (as references or requiring the same type) or concern their behavior when dealing with objects, which do not abide by the same rules as primitive values.