JavaScript Number

JavaScript Number

·

3 min read

As we all should know, “number” is a primitive javascript data type; It has an in-built object that helps us address the type and its values to different degrees. In this article, we’ll be talking about its operations, static and instance methods.

Firstly, let’s talk about the operations that apply to number in JavaScript.

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Modulus (%)

Useful JavaScript Static and Instance Methods And Their Uses

Number.isNaN()

This checks if the passed variable type is NaN or not. This is usually used for checking un-thrown errors in string conversion to number. If you try converting a letter to number, its value will be NaN. These are the kinds of error that this static method can catch.

console.log(Number.isNaN(Number("John"))); // Will log true

Number.isInteger()

This checks if the passed variable is an integer or not. This is used if we want to perform an operation on integers or non integers only. E.g. we want to save an amount (of money) to a database, it would make sense to check if it’s an integer before converting to floating point.

console.log(Number.isInteger(1.5)); // Will log false
console.log(Number.isInteger(1)); // Will log true

Number.isSafeInteger()

This checks if the passed variable is between -9007199254740992 and 9007199254740992 since everything in between is JavaScript’s safe number range. This can be used in situations where you’d want to calculate extremely large or extremely minute values. You’d check if it’s within the safe number range and calculate using bigint instead since bigInt was created for this purpose.

console.log(Number.isSafeInteger(9999999999)); // Logs true
console.log(Number.isSafeInteger(9999999999999999999)); // Logs false

Number.parseInt()

Converts string to integer values. This is mostly done is you want to perform number operations on the sequence of digits the string is made up of.

console.log(Number.parseInt("10")); // Logs 10
console.log(typeof Number.parseInt("10")); // Logs 'number'

Number.parseFloat()

Converts string to floating point values. This is mostly done is you want to perform floating operations on the sequence of digits the string is made up of or you need to save something like money to a database.

console.log(Number.parseInt("10.5")); // Logs 10
console.log(Number.parseFloat("10.5")); // Logs 10.5

This entails that you use parseFloat if you don't know the exact numeric type of the coming string; just to be safe.

Math.ceil()

This static method is used for rounding up floating point numbers.

console.log(Math.ceil(10.5)); // Logs 11

Number.floor()

This is used for rounding down floating point numbers.

console.log(Math.floor(10.5)); // Logs 10

Math.random()

This is used to get a floating point number with a random series of numbers after the decimal point.

console.log(Math.random()); // Logs a floating point number of  16 decimal places, example 0.8304488668582015

numVar.toSting()

This converts the number to its string representation. This is mostly done is you want to perform string operations on the sequence of digits the number is made up of.

const age = 40;
console.log(typeof age); // Logs Number
console.log(typeof age.toString()); // Logs String.

That brings us to the end of the article. Thanks for your time.