xxxxxxxxxx
let x = 123;
x.toString();
(123).toString();
(100 + 23).toString();
xxxxxxxxxx
var str = new String("OnePiece");
console.log(str.toString()); //OnePiece
//it returns a string representing the calling object.
//The toString() method can be used to convert a string object into a string.
// Example 1: Converting a Number to a String
var number = 42;
var numberString = number.toString(); // "42"
// Example 2: Converting an Array to a String
var fruits = ["apple", "banana", "cherry"];
var fruitsString = fruits.toString(); // "apple,banana,cherry"
// Example 3: Converting a Date to a String
var currentDate = new Date();
var dateString = currentDate.toString();
console.log("Date as a string: " + dateString); // "Mon Aug 15 2022 13:45:30 GMT+0000 (Coordinated Universal Time)"
xxxxxxxxxx
var myNumber=120;
var myString = myNumber.toString(); //converts number to string return: "120"
xxxxxxxxxx
toString .
For user-defined Function objects,
the toString method returns a string containing the source text segment
which was used to define the function. JavaScript calls the toString method
automatically when a Function is to be represented as a text value,
e.g. when a function is concatenated with a string.
xxxxxxxxxx
// Converting a value to a string in JavaScript
var value = 42;
var stringValue = String(value);
console.log(typeof stringValue); // Output: "string"
console.log(stringValue); // Output: "42"