Table of Content
The toString function in JavaScript converts a value to a string form without change. It works on numbers, arrays, and objects to show a readable text form of the value.
Understand the toString Function in JavaScript
The toString function converts the data of a variable to its string form. It returns a text version of the value that you can see or store.
Here is the syntax:
value.toString();You write it like this to avoid errors.
The toString function reads the value and returns its text form. This allows you to display the value or join it with other text.
The toString function converts a number to a text value. This lets you show the number as text or build new strings with it. It also converts an array to a text value of its items.
The Difference Between String() and toString()
String(value) creates a new string from the value. The value.toString() calls the method on the value itself.
Here is a table shows you key differences:
| String() | toString() |
|---|---|
| Works on any value even null or undefined | Fails on null or undefined |
| Creates a new string | Uses the object method |
| Called as a global function | Called as a method on a value |
Use String(value) for any type without risk of error. Use value.toString() for normal values like numbers, arrays, or objects.
Examples
Convert a Number to Text:
let num = 255;
let text = num.toString();
console.log(text);This converts the number 255 to its text form so you can display it as a string.
Convert an Array to Text:
let items = [10, 20, 30];
let text = items.toString();
console.log(text);This converts the array to one text line of items so you can store or show it.
Custom toString on an Object:
let user = {
name: "Ali",
toString: function() {
return "User: " + this.name;
}
};
console.log(user.toString());This defines a custom toString method on an object so it returns a custom text form.
String() vs toString():
let value = null;
console.log(String(value)); // "null"
console.log(value.toString()); // errorThis shows that String() works on null but toString() fails on null and throws an error.
Wrapping Up
You learned what the JavaScript toString function does and how to use it.
Here is a quick recap:
- The toString converts numbers, arrays, or objects to text form.
- String() works on any type, even null or undefined.
- Custom toString gives a special text form for your own objects.
FAQs
What is JavaScript toString function used for?
toString() function converts values into string form.
Example:
let num = 50;
console.log(num.toString());
// Output: "50"
It helps display numbers, arrays, and objects as text.How does toString work with numbers in JavaScript?
toString() method converts numbers into strings.
Example:
let num = 123;
let str = num.toString();
console.log(typeof str);
// Output: "string"
This method is useful when you want numeric values as text.What is the difference between String() and toString() in JavaScript?
String()can handlenullandundefined.toString()throws an error fornullorundefined.
console.log(String(null));
// Output: "null"
console.log(null.toString());
// Error: Cannot read properties of null
So, String() is safer for uncertain values.Can we use toString on arrays in JavaScript?
toString() method.
Example:
let arr = [1, 2, 3];
console.log(arr.toString());
// Output: "1,2,3"
It joins array elements into a single string separated by commas.Similar Reads
The this keyword links code, context, and object reference in JavaScript. You can use it to refer to the owner…
You need to compare values in almost every JavaScript program. This is where comparison operators come in. They check if…
The "do while" loop in JavaScript runs code once before checking the condition. Use it when the code must run…
The Math.pow() function in JavaScript solves one common task. It raises a number to a power. This helps avoid loops…
You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do…
The toSpliced function creates a new array without changing the original array in JavaScript. It returns a copy with new…
Math.asin() finds the arc sine of a number in JavaScript. It helps you work with angles from values. You get…
JavaScript switch statement checks many values without long chains. It appeared to replace stacked conditions that slow you down. Understand…
Data types in JavaScript help hold values and shape code rules. They set clear plans for text, numbers, and other…
Math.random() gives a number between 0 and 1 in JavaScript. It helps create random values for colors or other data.…