CRT-600 practice question 14 of 30

Given the code below: Which three code segments result in a correct conversion from number to string? Choose 3 answers

Full set: 223 questions
Answer options
A: let strValue = numValue. toString(); Correct answer
B: let strValue = * * 4 numValue; Correct answer
C: let strValue = numValue.toText ();
D: let scrValue = String(numValue); Correct answer
E: let strValue = (String)numValue;
Correct Answer:
A. let strValue = numValue. toString();; B. let strValue = * * 4 numValue;; D. let scrValue = String(numValue);
Explanation

In JavaScript a numeric value can be converted to its string representation by (1) invoking its own Number.prototype.toString() method, (2) calling the built-in String constructor as a function, or (3) forcing implicit coercion through concatenation with an empty string or a template literal.
Option A invokes toString(); option D calls String(numValue); option B uses a template literal (back-ticked ${numValue} ) which interpolates numValue and always returns a string. All three therefore return a correctly-typed string.

Why Incorrect

C. Number objects have no toText() method; call throws TypeError.
E. (String)numValue is not valid ECMAScript casting syntax; results in SyntaxError at parse time.

References

1. ECMA-262

14ᵗʰ Ed. (2023)

§21.1.3.29 “Number.prototype.toString” – describes numeric toString conversion.

2. ECMA-262

§19.1.1.1 “The String Constructor Called as a Function” – String(value) returns ToString(value).

3. ECMA-262

§7.1.17 “ToString Abstract Operation” – implicit coercion via concatenation/template literals.

4. ECMA-262

§12.2.9 Template Literals – evaluation always produces a String value.

5. Salesforce Lightning Web Components Dev Guide

“JavaScript Basics → Type Coercion”

para 4 – shows ‘’ + numValue and ${numValue} patterns for number-to-string conversion.

Scroll to Top