JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS toString()


📘 JavaScript toString()Convert Values to Strings

The toString() method in JavaScript is used to convert a value (like number, array, boolean, etc.) into a string.

It is a built-in method available on almost all data types.


🔹 Syntax
value.toString()

It returns a string representing the value of the object.


🔹 Examples by Data Type
✅ 1. Number to String
let num = 123;
let str = num.toString(); // "123"
✅ 2. Boolean to String
let flag = true;
let str = flag.toString(); // "true"
✅ 3. Array to String
let arr = [1, 2, 3];
let str = arr.toString(); // "1,2,3"
✅ 4. Date to String
let date = new Date();
let str = date.toString(); // e.g., "Wed Jul 10 2025 12:00:00 GMT+0530"
✅ 5. String to String
let name = "John";
let str = name.toString(); // "John"

❗Note:

  • toString() does not modify the original value, it returns a new string.

  • Works on most data types except null and undefined.

let x = null;
console.log(x.toString()); // ❌ Error: Cannot read properties of null

Practice Questions

Q1. How do you convert a number n = 150 to a string using toString()?

Q2. How do you convert a boolean value true into the string "true" using toString()?

Q3. What is the output when you apply toString() on the array [10, 20, 30]?

Q4. How do you convert today’s date from a Date object to a string format?

Q5. What does typeof (123).toString() return?

Q6. How do you store the string version of a variable x = false into a variable s using toString()?

Q7. How do you use toString() inside a console.log() to print a string from a number?

Q8. How do you convert a variable price = 999.99 to a string and store it in strPrice?

Q9. What happens when you try to use toString() on undefined or null?

Q10. How do you convert an array of strings like ["a", "b", "c"] to a single comma-separated string using toString()?


JS toString() Quiz

Q1: What does 123.toString() return?

A. 123
B. "123"
C. undefined
D. NaN

Q2: Which of these values will throw an error when using toString()?

A. 123
B. false
C. null
D. [1,2,3]

Q3: What will be the result of [1, 2, 3].toString()?

A. "1 2 3"
B. "1,2,3"
C. "123"
D. ["1","2","3"]

Q4: What is the result of (true).toString()?

A. true
B. "true"
C. undefined
D. 0

Q5: Which method converts a number to a string in JavaScript?

A. toInt()
B. String()
C. toString()
D. toChar()

Q6: Which data type does toString() return?

A. Number
B. Object
C. String
D. Boolean

Q7: What is the output of typeof (false).toString()?

A. boolean
B. false
C. string
D. object

Q8: Which of these will result in an error when calling toString()?

A. let x = "Hello"
B. let y = null
C. let z = 10
D. let a = [1, 2]

Q9: How can you safely convert any value to string in JavaScript?

A. Use toString() directly
B. Use JSON.stringify() always
C. Use String(value)
D. Don’t convert

Q10: What is the output of typeof (["a", "b"].toString())?

A. object
B. string
C. array
D. boolean

Go Back Top