JavaScript | toString( ) function

The toString() function in Javascript is used with a number and converts the number to a string. It is used to return a string representing the specified number object.

Syntax:

num.toString(base)

The toString() function is used with a number num as shown in above syntax using the ‘.’ operator. This function will convert num to a string.



Parameters Used: This function accepts a single optional parameter base. This parameter specifies the base in which the integer is represented in string. It is an integer between 2 and 36 which is used to specify the base for representing numeric values.

Return Value: The num.toString() method returns a string representing the specified number object.

Below are some examples to illustrate the working of toString() function in JavaScript:

  1. Converting a number to a string with base 2: To convert a number to a string with base 2 , we will have to call the toString() method by passing 2 as a parameter.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        var num=213;
        document.write("Output : " + num.toString(2));          
    </script>

    chevron_right

    
    

    Output:

    Output:11010101
  2. Converting a number to a string with base 8: To convert a number to a string with base 8, we will have to call the toString() method by passing 8 as a parameter.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        var num=213;
        document.write("Output : " + num.toString(8));          
    </script>

    chevron_right

    
    

    Output:

    Output : 325
  3. Converting a number to a string with base 16: To convert a number to a string with base 16, we will have to call the toString() method by passing 16 as a parameter.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

            
    <script type="text/javascript">
        var num=213;
        document.write("Output : " + num.toString(16));          
    </script>

    chevron_right

    
    

    Output:

    Output : d5

  4. When no parameter is passed: If the toString() method is called without passing any parameter then the number will be converted to string without change in BASE. Below is the program to illustrate this:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        var num=213;
        document.write("Output : " + num.toString());          
    </script>

    chevron_right

    
    

    Output:

    Output :213


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.