-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
toString()
– Convert Values to StringsThe 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.
value.toString()
It returns a string representing the value of the object.
let num = 123;
let str = num.toString(); // "123"
let flag = true;
let str = flag.toString(); // "true"
let arr = [1, 2, 3];
let str = arr.toString(); // "1,2,3"
let date = new Date();
let str = date.toString(); // e.g., "Wed Jul 10 2025 12:00:00 GMT+0530"
let name = "John";
let str = name.toString(); // "John"
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
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()
?
Q1: What does 123.toString() return?
Q2: Which of these values will throw an error when using toString()?
Q3: What will be the result of [1, 2, 3].toString()?
Q4: What is the result of (true).toString()?
Q5: Which method converts a number to a string in JavaScript?
Q6: Which data type does toString() return?
Q7: What is the output of typeof (false).toString()?
Q8: Which of these will result in an error when calling toString()?
Q9: How can you safely convert any value to string in JavaScript?
Q10: What is the output of typeof (["a", "b"].toString())?