banner



How To Change A Number To A String In Javascript

Management of Information is a crucial job for whatever programmer; JavaScript is a very versatile programming language which offers multiple built-in methods to convert data into different data types. The .tostring() is one of these methods. The .tostring() method can exist used to catechumen the data type of a variable from a number to a string. In this how-to guide nosotros will larn to catechumen a number into a string using the .tostring() method in JavaScript.

The .tostring() method is commonly used in combination with numbers to convert them into strings. It takes a single optional parameter:

Syntax:

Parameter: The .tostring() method takes a single optional parameter. This parameter volition specify the base of operations of the number which will be stored in the string.

Return Value: The .tostring() method returns a string which contains the specified number object converted to the specified base of operations.

How to alter a number into a cord

We just need to invoke the .toString() method without passing any parameters, if nosotros but want to correspond the number in its string class without changing its base of operations:

var num = 549 ;

var str = num.toString ( ) ;

console.log (num) ;

console.log (typeof num) ;

console.log (str) ;

console.log (typeof str) ;

How to convert a number to a string with base 2(binary)

If we want to catechumen a number into a cord as well as to a different base, allow's say two, and so we need to pass a parameter as well. In this case we volition pass ii as nosotros want to convert the number into binary:

var num = 549 ;

var str = num.toString ( 2 ) ;

console.log (str) ;

How to convert a number to a string with base 8(octal)

To convert the number into a cord likewise as into a base 8 number nosotros volition pass viii every bit a parameter:

var num = 549 ;

var str = num.toString ( viii ) ;

console.log (str) ;

How to convert a number to a string with base of operations sixteen(HexaDecimal)

Similarly to catechumen the number into a HexaDecimal while also converting it into a string we demand to invoke the .tostring() method and pass 16 equally a parameter to it:

var num = 549 ;

var str = num.toString ( xvi ) ;

panel.log (str) ;

How to Utilise Other Information Types with the toString() Method

The .toString() method is as well used to change information types other than numbers into their string representations.

Permit's expect at an example where an array has been converted into its string representation:

var arr = [ "John", "Jerry" ] ;

var str = arr.toString ( ) ;

panel.log (str) ;

Similarly we can convert any data type into a string:

var bool = true ;

var str = bool.toString ( ) ;

console.log (str) ;

console.log (typeof str) ;

Conclusion

The .toString() method takes a number and changes information technology into a cord. It takes a single optional parameter which can be used to convert the number to a different base. In JavaScript working with strings is a lot easier than numbers in some cases so converting a number into a cord is not an absurd idea.

In this guide we learned to convert a number into a cord data type in JavaScript using the .tostring() method. Moreover we also learned to convert other information types such as objects and booleans into strings too.

About the author

A Javascript Developer & Linux enthusiast with 4 years of industrial feel and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video cosmos.

Source: https://linuxhint.com/convert-number-to-string-javascript/

Posted by: gasparhossing.blogspot.com

0 Response to "How To Change A Number To A String In Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel