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.


Given the expression +true; !'Lydia'; what will be the outputs?

  1. 1 and false

  2. false and NaN

  3. false and false

  4. true and false

The correct answer is: 1 and false

To evaluate the provided expression, let's break it down step by step. Firstly, the expression uses the unary plus operator (`+`) with the boolean value `true`. The unary plus operator converts the operand to a number. In JavaScript, `true` evaluates to `1` when converted to a number. Therefore, the first part of the expression, `+true`, outputs `1`. Next, the expression includes the negation operator (`!`) applied to the string `'Lydia'`. In JavaScript, when a non-empty string is evaluated in a boolean context, it is considered `truthy`. Applying the negation operator to a truthy value (`'Lydia'`) will give `false`. Thus, `!'Lydia'` outputs `false`. Putting both outputs together, the expression `+true; !'Lydia';` results in the outputs `1` for the first part and `false` for the second. The correct answer aligns with this rationale, as it correctly summarizes the outputs of the expressions evaluated.