This is how you make numbers more readable in your JS code

Hello Folks 👋

What’s up friends, this is SnowBit here. I am a young passionate and self-taught frontend web developer and have an intention to become a successful developer.

Today, I am here with a cute and must to know tip for you as a JS …


This content originally appeared on DEV Community and was authored by DEV Community

Hello Folks 👋

What's up friends, this is SnowBit here. I am a young passionate and self-taught frontend web developer and have an intention to become a successful developer.

Today, I am here with a cute and must to know tip for you as a JS Developer.

Introduction

We all deal with numbers all day, some of them are little and short but some of them are just too large (10000000000)
big nums

Don't worry about writing them; you are at the right place. Keep your code clean by using the following tips...

Let's make numbers more readable

We generally use comma(,) to separate digits in a large number. But, this is Javascript you can't just use commas to separate digits.

const largeNumber = 1,000,000

If you use commas to separate digits in Javascript, you would have encountered this error 👇
err
err2

Let's separate digits without using commas

In Javascript, one can use underscores(_) to separate digits in numbers. Here's how you can do that.

const largeNumber = 1_000_000

output

Are you lazy like me?

I am too lazy to write all these long numbers while writing my code. So, I have a good idea of writing these long numbers that is a lot beneficial for writing clean code. Let me show you...

const largeNumber = 1e18
const secondLargeNumber = 4e12

const aLargeNumber = 1e18 + 4e12

Here, the pattern is

const n = [starting number] e [number of zeros]

Wanna try this, check out this fiddle

So, these were a few ways to make numbers more readable that you should use in your next project. Feel free to share more crazy ways to write numbers coming to your mind.

Thank you for reading, have a nice day!

Your appreciation is my motivation 😊 - Give it a like


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-02-23T15:10:59+00:00) This is how you make numbers more readable in your JS code. Retrieved from https://www.scien.cx/2022/02/23/this-is-how-you-make-numbers-more-readable-in-your-js-code/

MLA
" » This is how you make numbers more readable in your JS code." DEV Community | Sciencx - Wednesday February 23, 2022, https://www.scien.cx/2022/02/23/this-is-how-you-make-numbers-more-readable-in-your-js-code/
HARVARD
DEV Community | Sciencx Wednesday February 23, 2022 » This is how you make numbers more readable in your JS code., viewed ,<https://www.scien.cx/2022/02/23/this-is-how-you-make-numbers-more-readable-in-your-js-code/>
VANCOUVER
DEV Community | Sciencx - » This is how you make numbers more readable in your JS code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/23/this-is-how-you-make-numbers-more-readable-in-your-js-code/
CHICAGO
" » This is how you make numbers more readable in your JS code." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/02/23/this-is-how-you-make-numbers-more-readable-in-your-js-code/
IEEE
" » This is how you make numbers more readable in your JS code." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/02/23/this-is-how-you-make-numbers-more-readable-in-your-js-code/. [Accessed: ]
rf:citation
» This is how you make numbers more readable in your JS code | DEV Community | Sciencx | https://www.scien.cx/2022/02/23/this-is-how-you-make-numbers-more-readable-in-your-js-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.