toString() in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The toString() method is a JavaScript library function used to convert the given object to its corresponding string representation. toString in javascript can be used with numbers, strings, arrays, and objects.

Syntax of toString() in JavaScript

The following is the syntax of toString() in JavaScript:

The toString() function is simply applied to the value that needs to be converted to a string value.

Parameters of toString() in JavaScript

The toString() function in JavaScript does not take any parameter.

Return Value of toString() in JavaScript

Return Type: string

toString() function coverts the given value and returns its corresponding string representation or we can say that toString() function returns string value.

Example of toString() in JavaScript

Let's take a look at a basic example of how JavaScript toString() function is used: In this example, we are converting a number to the string value using the toString() function.

Output:

What is toString() in Javascript?

Suppose you have a list of numbers and you want to concatenate all those numbers, So for that, you can use toString() in javascript to convert all integers and concatenate all those numbers in a string value.

As the name suggests, the toString() function is a JavaScript library function used to convert the given object to its corresponding string representation, we can use the toString() function for converting numbers, strings, arrays, and objects to the corresponding string value.

How Does the toString() Function Work in JavaScript?

toString() in JavaScript receives an argument and returns the object’s text representation as a string value. The returned string value depends upon the type of argument that is being converted to string.

More Examples

Now we have learned about toString in javascript and now we can apply the toString() function in some of our examples:

Example 1: Converting an Object to a String Value

We can also apply the toString() function for the conversion of an object to its corresponding value.

In this example, we are making a number and string object and then will apply toString() function on those objects.

Input:

Output:

We can see that the objects are simply converted to their corresponding string values.

Example 2: Overriding the default toString() Method

If we are applying the toString in javascript to the object with key : value pair, then the toString() function will return [object Object], so we have to override the function with our default function in which we can write our function logic. The following code creates and assigns fun() function to override the default toString() method. This function generates a string containing the name and age of the object, in the form of key : value pair.

Input:

Output:

We can see that the object value is converted to string value according to our overridden function.

Example 3: Converting a Numeric Value to String Value

We can use toString() function to convert the numeric values to the corresponding string value. For that, we can simply apply the toString() function on the numeric value.

Input:

Output:

We can see that the numeric value is simply converted to a string value.

Example 4: Converting a Numeric Value to a String with Some Base Value

When dealing with numeric values, we can pass an optional parameter as the base value by which the integer is to be converted. The base is an integer value between 2 and 36 that specifies the base for conversion.

If we are not specifying the base value, the numeric value will just be converted to a string.

Input:

Output:

The numeric value is converted to the string depending on the base value we have passed in the parameter of the toString() function.

Example 5: Converting an Array to String Value

Now we will see how to convert an array to a particular String value. For that, we will simply apply the toString() function on the array, and String will be displayed.

Input:

Output:

In Output, comma-separated string values are printed on the console.

Example 6: Using toString() to Detect Object Class

We can also detect the class of the object using you need to call Function.prototype.call() and can pass the object you want to inspect for the class.

Input:

Output:

We can see that the program returns all the names of the classes whose objects we have made.

Conclusion

  • toString() function converts the given object to its corresponding string representation.
  • toString() function can be used with numbers, strings, arrays and objects.
  • toString() function does not take any parameter.
  • toString() function returns a string value.
  • We can override toString() function for converting object with key pair.
  • We can use toString() function to detect object class also.

See Also