- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the best way to convert a number to a string in JavaScript?
The best way to convert a number to a string is using the toString() method, with different bases. The method has an optional parameter “radix”, which is used as a base (2, 8, 16) for a numeric value.
Example
You can try to run the following code to convert a number to a string −
<!DOCTYPE html> <html> <body> <script> var num = 10; var val1 = num.toString(); var val2 = num.toString(2); var val3 = num.toString(8); var val4 = num.toString(16); document.write(val1 + "<br>" + val2 + "<br>" + val3 + "<br>" + val4); </script> </body> </html>
Output
10 1010 12 a
Advertisements