Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Number Methods in JavaScript?

JavaScript is a powerful language that caters to the needs of both beginners and experienced programmers. One of its many features is the ability to work with numbers and perform various operations. In this article, we will explore some JavaScript number methods that will help you manipulate and work with numbers more efficiently.

What are Number Methods?

Number methods in JavaScript are built-in functions that allow us to perform various operations and manipulations on numbers. These methods make it easier to work with numbers and perform complex calculations.

parseInt()

parseInt() is a built-in JavaScript function that takes a string as an input and returns an integer value by converting the string into an integer. If the string cannot be converted into an integer, it returns NaN (which stands for "Not a Number").

For example, let's say we have a string that contains the number 42:

const myString = "42";

To convert this string into an integer, you can use the parseInt() function as follows:

const myNumber = parseInt(myString);
console.log(myNumber); // Output: 42

The parseInt() function can also take an optional second argument, which is the radix or base of the number system you want to use for conversion. For example, if you want to convert a binary number represented as a string to an integer, you can pass 2 as the second argument:

const binaryString = "101";
const binaryNumber = parseInt(binaryString, 2);
console.log(binaryNumber); // Output: 5

parseFloat()

parseFloat() works similarly to parseInt(), but instead of converting a string into an integer, it converts it into a floating-point number (a number with a decimal point).

Here's an example:

const decimalString = "3.14";
const decimalNumber = parseFloat(decimalString);
console.log(decimalNumber); // Output: 3.14

Number()

The Number() function can also be used to convert a string into a number. However, unlike parseInt() and parseFloat(), the Number() function can convert a string into either an integer or a floating-point number, depending on the contents of the string.

const intString = "42";
const floatString = "3.14";

const intNumber = Number(intString);
const floatNumber = Number(floatString);

console.log(intNumber); // Output: 42
console.log(floatNumber); // Output: 3.14

It's important to note that if Number() cannot convert the string into a number, it will return NaN.

isNaN()

Sometimes, it's useful to check if a value is NaN (Not a Number). To do this, you can use the isNaN() function, which returns true if the value is NaN and false otherwise.

Here's an example:

const notNumber = "hello";
const checkNaN = isNaN(Number(notNumber));
console.log(checkNaN); // Output: true

toFixed()

When working with decimal numbers, you might want to limit the number of decimal places to a certain value. One way to achieve this is by using the toFixed() method. This method takes an integer as an argument, which represents the number of decimal places you want to keep. It then returns the number as a string with the specified number of decimal places.

For example, let's say we have a number with a lot of decimal places:

const longDecimal = 3.1415926535;

If we want to keep only two decimal places, we can use the toFixed() method like this:

const shortDecimal = longDecimal.toFixed(2);
console.log(shortDecimal); // Output: "3.14"

Note that the output is a string, not a number. If you want the output to be a number, you can use parseFloat() to convert it:

const shortDecimalNumber = parseFloat(shortDecimal);
console.log(shortDecimalNumber); // Output: 3.14

Math Object

In addition to the methods we discussed above, JavaScript also provides a built-in Math object that contains several useful methods and properties for working with numbers. Some of these methods include:

  • Math.round(): Rounds a number to the nearest integer.
  • Math.floor(): Rounds a number down to the nearest integer.
  • Math.ceil(): Rounds a number up to the nearest integer.
  • Math.sqrt(): Returns the square root of a number.
  • Math.pow(): Returns the base raised to the power of the exponent.
  • Math.max(): Returns the largest number from a list of numbers.
  • Math.min(): Returns the smallest number from a list of numbers.

Here are some examples of how to use these methods:

console.log(Math.round(3.5)); // Output: 4
console.log(Math.floor(3.5)); // Output: 3
console.log(Math.ceil(3.5)); // Output: 4
console.log(Math.sqrt(9)); // Output: 3
console.log(Math.pow(2, 3)); // Output: 8
console.log(Math.max(1, 2, 3, 4)); // Output: 4
console.log(Math.min(1, 2, 3, 4)); // Output: 1

Conclusion

In this article, we've learned about some essential JavaScript number methods and how to use them. These methods make it easier to work with numbers, perform calculations, and manipulate numeric data in various ways. By understanding these built-in functions, you'll be well-equipped to tackle a wide range of programming tasks that involve numbers. So keep experimenting, and happy coding!