What will be the output of the following JavaScript code: console.log(2 + '2');?

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

When evaluating the expression 2 + '2', JavaScript performs type coercion due to the presence of both a number and a string in the operation. In this scenario, the number 2 is combined with the string '2'.

JavaScript will attempt to convert the number into a string to accommodate the operation, as the + operator performs string concatenation when one of the operands is a string. Therefore, the number 2 gets converted to the string '2'.

After this conversion, the operation becomes '2' + '2', which results in the concatenated string '22'.

Thus, when the code console.log(2 + '2'); is executed, it prints '22' to the console. This explains why the output is the string '22' and confirms that the answer is indeed the first choice.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy