Getting To Know JavaScript Built-In Methods — Numbers & Math

Tolani Benson
The Startup
Published in
4 min readMay 14, 2020
Calculator
Photo by Charles Deluvio on Unsplash

This is the second post in my series about JavaScript’s built-in methods for different data types. There are a ton of handy ready-made methods which can be used to manipulate data, and I’ll be explaining some of the most useful ones.

You can see my first post about String methods, as well as a recap on JS data types here.

This post will cover the most useful Number and Math methods. I’ve bundled them together because a lot of the time you’re likely to be using Math operations on numbers, and there aren’t many specific Number methods that you’ll use often, so I’ve covered these last.

It’s also worth pointing out that when it comes to storing numbers, JavaScript isn’t great in terms of precision, which is why it a programming language that is used for programs which need high levels of precision such as financial software. This can result in some odd behaviour in some of these methods, which I have tried to highlight where relevant in the explanations below.

I’ll start with a few global methods which are related to numbers and pretty useful:

parseInt(string, base)

Converts a string to a number, using the numerical base in the second parameter. That can sound a bit confusing if you’re not mathsy and don’t have a good idea of what the numerical base you want is! Mainly, you’ll probably want to return a decimal value, in which case use 10 as the second parameter.

The second argument is optional, but if you omit it it can cause some strange outputs depending on what string is passed in, eg if the base parameter is omitted, and the string begins with “0x”, JavaScript assumes the base is 16 (hexadecimal).

I’d recommend always setting the second parameter to avoid unexpected outputs. You can read more about the different options for the base in the docs here.

If the string argument contains multiple space-separated numbers, it will only return the first number and ignore the rest of the string. If the first character in the string is not a number it will return NaN.

parseFloat(string)

Works the same as parseInt, but doesn’t need the radix as floats are decimals!

isNaN(value)

Returns a boolean, evaluates whether the value provided is a number or not. True if the value is not a number.

eval(string)

I’m putting this here with the caveat to NEVER USE EVAL! It evaluates whatever is passed in as a string, which means it is very easy to be subject to malicious attacks. However, having said that, if you’re building some very basic calculator, which won’t be hosted and you’re just messing around practising your basic JS and CSS skills, it’s an easy one to use! But to be honest if you’re practising your JS you may as well practise it properly and build out the functionality instead of using this shortcut that you’d never use in real life!

JavaScript Math Methods

These are the main built-in methods you’ll probably actually use when working with numbers!

All of these will work if the argument is a number passed in as a string, but will always return a Number object.

Math.random()

Returns a random floating point number between 0 & 1.

Math.max(values)

Returns the largest number of the values passed in. If wanting find the highest value in an array, use the spread operator (…) to deconstruct the array or it will return NaN. Eg if we had a variable nums assigned to an array [1,2,3,4,5] we would write it like this Math.Max(…nums).

Math.min(values)

Returns the smallest number of the values given. Works the same as Math.Max().

Math.round(num)

Returns the provided number rounded to the closest integer (whole number).

Math.floor(num)

Rounds down to the previous integer.

Math.ceil(num)

Rounds up to the next integer.

Math.trunc(num)

Does not mathematically round. Returns the provided number as an integer, cutting off any digits after the decimal point.

Math.abs(num)

Returns the absolute version of the number passed in (ie returns a positive number, or 0). Eg Math.abs(5) returns 5, Math.abs(-5) returns 5, Math.abs(0) returns 0.

Math.sign(num)

Returns 1, -1 or 0 to indicate the sign of the number provided. Eg Math.sign(5) returns 1, Math.sign(-5) returns -1, Math.sign(0) returns 0.

Math.pow(num, x)

Returns num to the power of x (numˣ).

Math.sqrt(x) & Math.cbrt(x)

Returns the square root of x and the cube root of x, respectively.

Math.exp(x) & Math.log(x)

Returns eˣ and logₓ respectively.

Math.PI

Not technically a method, but a property! Handy if you need Pi — there are a bunch more properties to give you specific mathematical values which you can check out here.

JavaScript Number Methods

There aren’t many super useful ones, here are two you might use!

num.toFixed(decimalPlaces)

This will return the provided number as a string to the given number of decimal places. In general it rounds up or down but it can have strange results around numbers ending in 5 because of the issues JS has with precision. Better to use the more precise Math.round() if rounding to an integer (0 decimal places). If no argument is given this will return the number exactly as passed in, but as a string.

num.toPrecision(digits)

Returns a number as a string to the specified number of significant digits — ie the number of digits after the first non-zero number. This is not the same as decimal places! Eg (0.005).toPrecision(3) will return “0.00500”, and (12345).toPrecision(3) will return “1.23e+5”.

Links to the rest of the Getting To Know JavaScript Built-In Methods series:

--

--

Tolani Benson
The Startup

Ex-financial analyst turned software engineer. Lover of learning, coding, cake and dogs. Not in that order.