All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Number Format Comma

Last Updated : Mar 11, 2024

JavaScript Number Format Comma

In this tutorial we will show you the solution of JavaScript number format comma, we had two options for that using ‘toLocaleString() and regular expressions’.

Here we used ‘toLocaleString()’ method it is a built-in method of Number object that returns a locale-sensitive string representing the number with parameter ‘en-US’.

So finally we achieve result of numbers with comma format.

Step By Step Guide On JavaScript Number Format Comma :-

Here we defined a variable using ‘let’ keyword with initialization. Then we using ‘toLocaleString()’ method with defined value and parameter ‘en-US’.

So it will changing given input numeric value to comma format and stored to variable ‘str’.

Result printed on console using ‘console.log()’ method. The console.log() method prints any message that need to be displayed to the user.

It accepts a parameter which can be an array, an object or any message and it returns the value of the parameter given.

<!DOCTYPE html>
<html>
    <head>
        <title>number comma format</title>
    </head>
    <body>
        <script>
            let n=38432927357;
            let str=n.toLocaleString("en-US");
            console.log(str);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Within <script> tag we defined a variable ‘n’ with value ‘38432927357’. Then variable n appends with ‘toLocaleString()’ method for convert numeric value into comma format which is done by passed parameter ‘en-US’.
  7. This parameter used to create thousand separators for given number and result value stored to variable ‘str’.
  8. Result printed using ‘console.log()’ method. Console.log() is a function in javascript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Its output display on the javascript console.
  9. In browser for open javascript console use the shortcut ‘ctrl+shift+j’ then console panel will open at right hand side there we can see our result.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion we are able to know how to create comma format number in javascript.

When we execute our program on browser we can’t see the result so we have to use shortcut ‘ctrl+shift+j’ then result we can see converted value will displayed on console by console.log().

The console.log() method only will print any message or value on the console, for general printing, value or string on webpage in javascript we had many inbuilt methods.

We will see about them in future. I hope this tutorial on JavaScript number format comma helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪