Like any kind of apps, JavaScript apps also have to be written well.
Otherwise, we run into all kinds of issues later on.
In this article, we’ll look at some tips we should follow to write JavaScript code faster and better.
Check if a Number is Finite
The isFinite
method lets us check if a value is a finite number or not.
For instance:
Number.isFinite(1)
returns true
.
On the other hand:
Number.isFinite(1)
returns false
.
Format a Decimal Number into a String
We can use the toFixed
method to format a decimal number into a string.
For instance, we can write:
(20.22).toFixed()
and get '20'
.
Also, we can pass in the number of decimal digits we want to keep by writing:
(20.22).toFixed(1)
and get “20.2”
.
Format a Number into a Language Sensitive String
The toLocaleString
method lets us get a string that has a number formatted according to the locale.
For instance, we can write:
(20.22).toLocaleString('fr-ca')
Then we get “20,22”
.
Format a Number into Exponential Notation
The toExponential
method lets us convert a number into a string that’s in exponential notation.
For instance, we can write:
(21.22).toExponential()
Then we get “2.122e+1”
.
Change the Number of Decimal Digits of a Number
The toPrecision
method formats a number into a string that has the given number of decimal digits.
For instance, we can write:
(21.21).toPrecision(5)
Then we get “21.210”
.
We can also pass in a number that’s smaller than the number of decimal digits in the number.
Then we get a string with the number formatted into exponential notation.
For instance, we can write:
(21.21).toPrecision(1)
And we get “2e+1”
.
Converting a Number to a String
The toString
method converts a number to a string.
The argument it takes is the radix.
For instance, if we want to convert the decimal number 10 to binary, we can write:
(10).toString(2)
Then it returns “1010"
.
Parsing a String into an Integer
We can parse a string into an integer with the parseInt
method.
For instance, we can write:
Number.parseInt('10.00')
and we get back 10.
It also takes a second argument with the radix.
So if we want to convert it to a given base, we can do that by writing:
Number.parseInt('16', 16)
Then we get 22 since we converted '16'
to a base 16 number.
Parsing a String into a Floating Point Number
There’s the Number.parseFloat
method to convert a string into a floating-point number.
For instance, we can write:
Number.parseFloat('10.00')
and get 10.
It can also convert a string to a number if it starts with a number by removing the non-numerical parts and converting the rest to a number.
For example, we can write:
Number.parseFloat('36 bottles')
Then we get 36.
Check if a Number is Within a Safe Range
In JavaScript, a number is considered to be safe if it’s between -2 ** 53
and 2 ** 53
.
To check that, we can use the Number.isSafeInteger
method.
For instance, we can write:
Number.isSafeInteger(2 ** 53)
which returns true
, and:
Number.isSafeInteger(2 ** 53 + 1)
which is false
.
Checking if a Value is NaN
Checking for NaN
is tricky.
Therefore, we should use the Number.isNaN
to do it since we can’t use ===
to do the comparison.
For instance, we can write:
Number.isNaN(NaN)
and that would return true
.
Also:
Number.isNaN('foo' / 'bar')
returns true
.
Check if a value is an Integer
The isInteger
method lets us check if a value is an integer.
We can use it by writing:
Number.isInteger(1)
which returns true
, or:
Number.isInteger(0.2)
which returns false
.
Trimming the beginning of a String
We can remove whitespace from the beginning of a string with the trimStart
method.
For instance, we can write:
' foo'.trimStart()
Then it returns “foo”
.
Trimming the End of a String
There’s also the trimEnd
method to remove whitespace from the end of a string.
For example, we write:
'foo '.trimStart()
Then it returns “foo”
.
Trimming Both Ends of a String
The trim
method will remove whitespace from both ends of a string.
For instance, we can write:
' foo '.trim()
Then it returns 'foo'
.
Conclusion
There are various ways to parse and check numbers.
Also, JavaScript strings have methods to remove whitespaces.