JavaScript Number and Math

Durjoy
3 min readNov 2, 2020

JavaScript Number

JavaScript Number is a primitive wrapper object used to represent and manipulate number like 52 or -13.5. The Number constructor contains constants and method to work with numbers.

Value of other types can be converted into numbers.

Static Methods of Number:

Number.isNaN(): The full form of NaN is ‘Not a Number’. Number.isNan() determine whether the passed value is NaN.

The syntax of this method is Number.isNaN(value). The parameter value is the value we pass to test whether it is NaN. The return value of the method is true or false. If the given value is a number it will return false and if the given value is not a number it will return true.

Number.parseFloat(): The Number.parseFloat() method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN.

The syntax of this method is Number.parseFloat(string). The parameter string is the value to parse. If this argument is not a string then it is converted to string by ToString abstract operation. Leading whitespace in this argument is ignored. The return value of this method is a floating point number or NaN. NaN is returned when the first non-whitespace character cannot be converted to a number.

Number.parseInt(): The Number.parseInt() method parses a string argument and returns an integer of the specified radix or base. The syntax of this method is Number.parseInt(string,[ radix]). The parameter string is the value to parse. If this argument is not a string then it is converted to string by ToString abstract operation. Leading whitespace in this argument is ignored. Radix is an optional parameter. An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string. This does not default to 10. The return value of this method is an integer. If the radix is smaller than 2 or bigger than 36, and the first non-whitespace character cannot be converted to a number, NaN is returned.

JavaScript Math

Math is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object. Math works with the Number type. It doesn't work with BigInt.

Static Methods of Math:

Math.abs(): It returns the absolute value of a number.

The syntax of this method is Math.abs(x). The parameter x is a number. The return value of this method is the absolute value of the given number.

Math.ceil(): The Math.ceil() function always rounds a number up to the next largest integer. If we pass null inside this method, we get 0 not NaN.

The syntax of this method is Math.ceil(x). The parameter x is a number. The return value of this method is the smallest integer greater than or equal to the given number.

Math.floor(): This function returns the largest integer less than or equal to a given number.

The syntax of this method is Math.floor(x). The parameter x is a number. The return value of this method is the largest integer less than or equal to the given number.

Math.min(): This function returns the lowest-valued number passed into it, or NaN if any parameter isn't a number and can't be converted into one.

The syntax of this method is Math.min([value1[, value2[, ...]]]). The return value of this method is the smallest of the given numbers. If any one or more of the parameters cannot be converted into a number, NaN is returned. The result is infinity if no parameters are provided.

Math.max(): The Math.max() function returns the largest of the zero or more numbers given as input parameters.

The syntax of this method is Math.max([value1[, value2[, ...]]]). The parameters are Number. The return value of this method is The largest of the given numbers. If any of the arguments are NaN or cannot be converted to a number, NaN is returned.

Math.random(): This function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

The syntax of this method is Math.random(). The return value of this method is a floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).

Math.round(): This function returns the value of a number rounded to the nearest integer.

The syntax of this method is Math.round(x). The parameter x is a number. The return value of this method is the given number rounded to the nearest integer.

--

--