Which of the following casting rules is incorrect when concatenating?

Study for the JavaScript Certification Test. Utilizes flashcards and multiple-choice questions; each question includes hints and explanations. Prepare to ace your exam!

When it comes to concatenation in JavaScript, it's important to understand how different data types are coerced into strings. The correct statement regarding the casting rules involves recognizing how JavaScript handles boolean and numeric values during the concatenation process.

The statement that "9 + true equals '9true'" is indeed incorrect. When you use the addition operator with a number and a boolean, JavaScript first converts the boolean value to a number before performing the operation. In the case of true, it's converted to 1. Therefore, the expression 9 + true actually evaluates to 9 + 1, which equals 10. If we were to concatenate 9 with a string representation of true (like "true"), we would need to use the string explicitly.

In contrast, examining the other statements provides a deeper understanding of JavaScript's type coercion:

  • The combination of false + 1 results in 1 because false is coerced to 0, leading to the operation 0 + 1.
  • For 1 + null, null is treated as 0, thus the result is 1 + 0 which equals 1.
  • Lastly, concatenating `"" +
Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy