/  Technology   /  Int to String conversion in JavaScript

Int to String conversion in JavaScript


In JavaScript numbers can be converted into string using the toString() method. The toString method allows us to convert a given number to a string. The optional base parameter can be used to specify the base in which the number will be represented.

The toString() is the built-in method of the JavaScript that converts a number into a string.

Int to string.html

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>int to String conversion</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
// The toString() method can also convert floating and negative numbers as shown below:
        var num = 24;
        var str = num.toString();
        console.log(num); // 24
        console.log(str); // "24"
        24.toString(); // Error: Invalid or unexpected token
        (24).toString(); // "24"
        (9.7).toString(); // "9.7"
        (-20).toString(); // "-20"
        var num = 15;
        num.toString(); //"15"
        num.toString(2); //"1111"(binary)        
        num.toString(8); //"17"(octa)
        num.toString(16); //"f"(hexa)

Output of the code is presented in comments.

Leave a comment