Sunday, September 22, 2024
Google search engine
HomeLanguagesJavascriptJavaScript String toString() Method

JavaScript String toString() Method

The JavaScript toString() method is an inbuilt function that returns the string itself. It does not modify the original string and it can be used to convert a string object to its string representation.

Syntax:

string.toString()

Parameters: It does not accept any parameter.

Return Values: It returns a new string representing the given string object.

Below is an example of the string.toString() Method. 

Example 1: This example shows the basic use of the string.toString() Method in Javascript. 

javascript




// Take string as input and display it
// with the help of string.toString()
// Method
let a = new String("GfG");
console.log(a.toString());


Output:  

GfG

Example 2: Taking a string as input and printing it with the help of a string.toString() method.

javascript




// Taking a string as input and printing it
// with the help of string.toString() method
let a = new String("neveropen");
console.log(a.toString());


Output: 

neveropen

Example 3: Taking a string as input and printing it with the help of string.toString() method.

javascript




// Taking a string as input and printing it
// with the help of string.toString() method
let a = new String("GFGGFG");
console.log(a.toString());


Output: 

GfGGfG

Example 4: In this example, we are converts the variable number to a string using the toString() method and output the result. The typeof operator confirms the type of our output as “string”.

Javascript




let number = 42;
let result = number.toString();
console.log(result);
console.log(typeof(result));


Output

42
string

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browsers: 

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments