Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Salesforce JS-Dev-101 Exam - Topic 4 Question 3 Discussion

Actual exam question for Salesforce's JS-Dev-101 exam
Question #: 3
Topic #: 4
[All JS-Dev-101 Questions]

Given the code below:

let numValue = 1982;

Which three code segments result in a correct conversion from number to string?

Show Suggested Answer Hide Answer
Suggested Answer: B, C, D

Comprehensive and Detailed Explanation From JavaScript Knowledge:

We want to convert the number 1982 to a string.

Check each option:

A . numValue.toText()

There is no standard toText() method on numbers.

This will result in TypeError: numValue.toText is not a function.

B . String(numValue);

String() as a function converts its argument to a string.

String(1982) returns '1982'.

This is correct.

C . '' + numValue;

'' is a string; + with a string operand performs string concatenation.

'' + 1982 '1982'.

This is a common shorthand for number-to-string conversion.

D . numValue.toString();

Number.prototype.toString() converts the number to its string representation.

1982..toString() or (1982).toString() returns '1982'.

For the variable, numValue.toString() is valid: '1982'.

E . (String)numValue;

This is not valid JavaScript casting syntax; it is more like a C/Java-style cast.

In JavaScript, that is parsed as a grouping expression (String) and then numValue; it does not convert numValue to a string.

Thus the correct answers are:

B . String(numValue);

C . '' + numValue;

D . numValue.toString();

Relevant concepts: primitive type conversion, String() casting, .toString(), coercion via + with strings.

________________________________________


Contribute your Thoughts:

0/2000 characters
Amalia
23 hours ago
Wait, can you really convert like that?
upvoted 0 times
...
Gail
6 days ago
I thought A might work, but guess not!
upvoted 0 times
...
Danilo
11 days ago
A and E are definitely wrong.
upvoted 0 times
...
Simona
16 days ago
B, C, and D are correct!
upvoted 0 times
...
Johnna
22 days ago
I'm a bit confused about option A; I don't think `toText()` is a real method, but I can't recall all the details.
upvoted 0 times
...
Mose
27 days ago
I practiced a similar question, and I feel like `'' + numValue` is a common trick for conversion.
upvoted 0 times
...
Theodora
1 month ago
I think `numValue.toString()` is definitely a valid way to convert a number to a string.
upvoted 0 times
...
Jaclyn
1 month ago
I remember that using `String(numValue)` should work, but I'm not sure about the others.
upvoted 0 times
...

Save Cancel