Single Choice Not Set

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?

ID: #4841 Basic JavaScript MCQ 226 views
Question Info
#4841Q ID
Not SetDifficulty
Basic JavaScript MCQTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A toString()
  • B String(human)
  • C String newvariable="human"
  • D Both human.toString() and String(human)
Correct Answer

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;
console.log(x.toString()); // Output: "123"
let y = true;
console.log(y.toString()); // Output: "true"
"" + value:
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.