The JavaScript toString function is an object method that converts the specified object to a string. We can use the toString() function on strings, arrays, numbers, or any object, and it returns the string representation of the given object.
In this article, we will explain the JavaScript toString function in relation to strings, dates, arrays, and numbers, providing examples. The syntax of the object toString() method is
Object.toString();
Return Value: The toString() function does not accept any parameter values and returns the string representation of the given object.
JavaScript string toString
The basic syntax of the string toString function is
String_Object.toString()
Return Value: The toString function does not accept any arguments and returns the string representation.
JavaScript toString string example
In the following example, we use the toString() method to return the string representation of a string object.
const s = new String("New Year");
console.log(s.toString());
New Year
Example 2:
First, we declared three variables, Str1, Str2, and Str3, and assigned corresponding values using the first three statements.
The following three JavaScript statements will convert the decimal and integer values to string values. Next, we used the typeOf String functions to show you the Data type of the variable.
The screenshot below shows that Str2 (decimal) and Str3 (integer) data types are converted to a string type.
<!DOCTYPE html>
<html>
<head>
<title> ToStringJavaScript </title>
</head>
<body>
<h1> JavaScriptToString </h1>
<script>
var Str1 = "Tutorial Gateway";
var Str2 = 123.45;
var Str3 = 4567;
var Str4 = Str1.toString();
var Str5 = Str2.toString();
var Str6 = Str3.toString();
document.write(Str4 + "<br \>");
document.write(Str5 + "<br \>");
document.write(Str6 + "<br \>");
document.write(typeof(Str5) + "<br \>");
document.write(typeof(Str6) );
</script>
</body>
</html>

JavaScript Number toString()
If we use the toString() function on numeric values, it converts the given number to a string.
The syntax of the Number toString() method is
Number.toString(base)
Parameter: It accepts an optional base value, and the default value is 10. It is an integer between 2 and 32 representing the base to represent the given value.
Return Value: The Number.toString() method returns the string representation of a given number.
JavaScript toString Number example
In the following example, we use the toString() method to convert a number to a string.
const n = 50;
let s = n.toString();
console.log(s);
50
Example 2: In this program, we use the toString() method with a base value of 2. It means the JavaScript example converts a given number to a string with base 2. If you convert the binary number output to decimal, it returns 50 (32 + 16 + 2 = 50).
const n = 50;
let s = n.toString(2);
console.log(s);
110010
Example 3: In this program, we use the toString() method to convert a given number to a string with base 8.
const n = 50;
let s = n.toString(8);
console.log(s);
62
Example 4: In this example, we use the toString() function to convert a given number to a string with base 16.
const n = 50;
let s = n.toString(16);
console.log(s);
32
Example 5: If we use the toString() function to display the string representation of 0 and -0, it returns “0”.
let a = 0;
let b = -0
console.log(a.toString());
console.log(b.toString());
0
0
Example 6: For infinity and NaN values, it returns Infinity and NaN as the string representation.
let a = Infinity;
let b = -Infinity;
let c = NaN
console.log(a.toString());
console.log(b.toString());
console.log(c.toString());
Infinity
-Infinity
NaN
JavaScript Array toString()
The Array.toString() method returns a string representation of the array items separated by a comma.
The syntax of the array toString method is
Array.toString()
Return Value: The tostring() method does not accept any parameters and converts the given array to a string, where comma joins each element. It does not change the original array; instead, it returns a new string with comma-separated array items. If a given array is empty, the toString() method returns an empty string.
JavaScript toString array example
In the following example, we declared an array of three strings. Next, we used toString() to convert an array into a string of elements separated by a comma.
let a = ["USA", "UK", "INDIA"];
let s = a.toString();
console.log(a);
console.log(s);
(3) ['USA', 'UK', 'INDIA']
USA,UK,INDIA
Example 2: This code uses the toString() on an array of integer values to convert the integer array to a string.
let a = [1, 3, 5, 7, 9];
let s = a.toString();
console.log(a);
console.log(s);
(5) [1, 3, 5, 7, 9]
1,3,5,7,9
Example 3: In this example, we use the toString() method to convert the matrix or two-dimensional array to a string.
let a = [[1, 3, 5], [7, 8, 9], [11, 13, 15]];
let s = a.toString();
console.log(s);
1,3,5,7,8,9,11,13,15
Example 4: Using the toString() method on a mixed array to return the string representation of it.
let a = [1, 'hi', 3, 'a', 5.7, 15];
let s = a.toString();
console.log(s);
1,hi,3,a,5.7,15
JavaScript Date toString()
The syntax of the date toString()method is
Date.toString()
Return Value: It does not accept any parameter value and returns the string representation of the given date and time.
NOTE: For the date part, use toDateString() and for the time part, use toTimeString(). To display date in UTC format, use toUTCString() and for local system format, use toLocaleString() method.
JavaScript toString Date example
In the following example, we use the toString() method to return the string representation of the current date and time.
const dt= new Date();
console.log(dt);
console.log(dt.toString());
Wed Jun 17 2026 12:53:11 GMT+0530 (India Standard Time)
Wed Jun 17 2026 12:53:11 GMT+0530 (India Standard Time)
Example 2: In this example, we use the Date() constructor to parse the current date and use the toString() method to display the custom date and time in string format.
const dt= new Date(1947, 7, 15, 10, 15, 11);
console.log(dt.toString());
Fri Aug 15 1947 10:15:11 GMT+0530 (India Standard Time)
Example 3: If we use the toString() method on a custom date without any time information, it returns the default time according to the local time zone. As the system’s local time is 5 hours 30 minutes ahead, it adds that number to the default 00:00:00.
const dt= new Date("2026-01-01");
console.log(dt.toString());
Thu Jan 01 2026 05:30:00 GMT+0530 (India Standard Time)
Use toString to convert Boolean values to a string
We can use the toString() function to convert a Boolean true or false value to a string.
const flag = true;
console.log(flag.toString());
console.log(true.toString());
console.log(false.toString());
true
true
false
JavaScript Object toString()
As we mentioned earlier, the object class has a toString() function, but the return value is different from strings, arrays, or Boolean values.
For instance, if we use the toString () function to convert the given object to a string rather than display the information, it returns [object, object].
const obj = {
name: "John",
age: 30,
Salary: 100000
};
console.log(obj.toString());
[object Object]
To see the object content, we must use JSON.stringify() function. It displays the content inside the object.
console.log(JSON.stringify(obj));
{"name":"John","age":30,"Salary":100000}
Otherwise, we must customize the toString() method to display the content inside the object.
const obj = {
name: "John",
age: 30,
Salary: 100000,
toString() {
return `${this.name}: At the Age of ${this.age}, earning ${this.Salary}`;
}
};
console.log(obj.toString());
John: At the Age of 30, earning 100000
What happens when we use toString() on null and undefined values?
If we use the JavaScript toString() function on null or undefined values, it returns a TypeError. As we all know, the base class of the toString() method is an object, and it requires an object or any other primitive under the object. As null and undefined are special cases and don’t come under object, it returns TypeError.
console.log(null.toString());
console.log(undefined.toString());
TypeError: Cannot read properties of null (reading 'toString')
Convert an array to a string using toString vs join()?
Both the toString() method and the join() function convert the given array to a string. However, the toString() function separates each array element using a comma. On the other hand, we can define the separator inside the join() method. Here, we used a whitespace to join the array elements.
let a = ["USA", "UK", "INDIA"];
let s = a.toString();
let j = a.join(' ');
console.log(s);
console.log(j);
USA,UK,INDIA
USA UK INDIA