📊 JavaScript
Q. Assume that we have to convert “false” that is a non-string to string. The command that we use is (without invoking the “new” operator)
  • (A) String newvariable=”false”
  • (B) Both false.toString() and String(false)
  • (C) String(false)
  • (D) false.toString()
💬 Discuss
✅ Correct Answer: (B) Both false.toString() and String(false)

Explanation:

In JavaScript, we can convert a boolean (false) to a string in two ways:

Using .toString() method

  • The .toString() method converts false to the string "false".
  • It works only if false is a primitive value, not null or undefined.

Using String() function

  • The String() function also converts false to a string.
  • It works with any value (null, undefined, numbers, booleans, etc.).

Why Not Other Options?

  • (A) String newvariable=”false” → ❌ Incorrect, because this is not valid JavaScript syntax.
  • (C) String(false) → ✅ Correct, but not the only correct answer.
  • (D) false.toString() → ✅ Correct, but not the only correct answer.

Since both methods work, the best answer is (B) Both false.toString() and String(false).

var str2 = String(false);
console.log(str2); // Output: "false"
var str1 = false.toString();
console.log(str1); // Output: "false"

Explanation by: Mr. Dubey

In JavaScript, we can convert a boolean (false) to a string in two ways:

Using .toString() method

  • The .toString() method converts false to the string "false".
  • It works only if false is a primitive value, not null or undefined.

Using String() function

  • The String() function also converts false to a string.
  • It works with any value (null, undefined, numbers, booleans, etc.).

Why Not Other Options?

  • (A) String newvariable=”false” → ❌ Incorrect, because this is not valid JavaScript syntax.
  • (C) String(false) → ✅ Correct, but not the only correct answer.
  • (D) false.toString() → ✅ Correct, but not the only correct answer.

Since both methods work, the best answer is (B) Both false.toString() and String(false).

var str2 = String(false);
console.log(str2); // Output: "false"
var str1 = false.toString();
console.log(str1); // Output: "false"

💬 Discussion


📊 Question Analytics

👁️
199
Total Visits
📽️
4 y ago
Published
🎖️
Kirti
Publisher
📈
81%
Success Rate