JavaScript toString and Number Conversion Explained
Numbers and strings don't always play nice. See how toString() and Number() convert between them, including the edge cases you'll actually run into.

Contents
Using JavaScript toString and Number
JavaScript toString and Number — when you use JavaScript functions, they generally use the values assigned to their parameters correctly under normal conditions. But in some cases the opposite happens. That is, when you want to assign a value to a parameter, you’ll need to use a numeric value as a string or a numeric value as a number. We call these kinds of type changes conversions. Now let’s look at this situation with a few examples below.
JavaScript toString
The toString function lets you convert a numeric value into string, that is, text format. This function is frequently used in the JavaScript programming language and helps us convert a given numeric value into text. For example, if a number’s value is 123, toString can convert that value into the string “123”. This function lets us use data in string format for various purposes, such as displaying data visually or saving it to a database. As an example:
var sayi = 25;
console.log(sayi.toString());
//The console output will be as follows.
//"25"
JavaScript Number
The Number function lets you convert a value in text format into numeric format.
var metin ="25";
var yeni_format= Number(metin);
console.log(yeni_format);
//The screen output will be as follows.
//25
If you look closely at the examples above, the screen outputs are strings and the values are in double quotes. However, numeric, that is integer, values have no double quotes.
- Mozilla Developer Network (MDN): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
- W3Schools: https://www.w3schools.com/jsref/jsref_shift.asp
Share it if it helped



