QLet's say we want to turn the text "human" from a text file into a string without utilising the "new" operator. Which of the following is the proper method to accomplish it?
Question Info
Choose the Best Option
Click any option to instantly check if you're correct.
Explanation
There are several ways to convert a value to a string in JavaScript.
Here are a few examples:
value.toString():
This method is available on many JavaScript objects and it returns a string representation of the object.
For example:
let x = 123;
"" + value:
console.log(x.toString()); // Output: "123"
let y = true;
console.log(y.toString()); // Output: "true"
This is known as string concatenation,
and it converts the value to a string by concatenating it with an empty string.
For example:
let x = 123;
console.log("" + x); // Output: "123"
let y = true;
console.log("" + y); // Output: "true"
String(value):
This is the String constructor, and it converts the value to a string by calling the toString() method on it.
For example:
let x = 123;
console.log(String(x)); // Output: "123"
let y = true;
console.log(String(y)); // Output: "true"<
All of these methods can be used to convert a value to a string without using the new operator.
Share This Question
Challenge a friend or share with your study group.