Skip to main content

Javascript Number Methods

JavaScript provides various built-in methods for working with numbers.

Here are some of the most commonly used ones:

  • toString(): Converts a number to a string.
let num = 42;
console.log(num.toString()); // "42"
  • toFixed(): Formats a number with a specified number of decimal places and returns a string representation of the result.
let num = 3.14159;
console.log(num.toFixed(2)); // "3.14"
  • parseInt(): Converts a string to an integer.
let str = "42";
console.log(parseInt(str)); // 42
  • parseFloat(): Converts a string to a floating-point number.
let str = "3.14159";
console.log(parseFloat(str)); // 3.14159
  • isNaN(): Determines whether a value is NaN (Not-a-Number).
let num = "hello";
console.log(isNaN(num)); // true
  • Math.round(): Rounds a number to the nearest integer.
let num = 3.7;
console.log(Math.round(num)); // 4
  • Math.max(): Returns the largest number in a list of arguments.
console.log(Math.max(1, 2, 3)); // 3
  • Math.min(): Returns the smallest number in a list of arguments.
console.log(Math.min(1, 2, 3)); // 1
  • Math.random(): Returns a random number between 0 and 1.
console.log(Math.random()); // 0.123456789
  • toExponential(): This method returns a string representation of a number in exponential notation.
let x = 123456;
console.log(x.toExponential(2)); // "1.23e+5"
  • toPrecision(): This method returns a string representation of a number with a specified number of total digits.
let x = 1234.56;
console.log(x.toPrecision(3)); // "1.23e+3"
  • isFinite(): This method determines whether a value is a finite number.
console.log(isFinite(123)); // true
console.log(isFinite(Infinity)); // false
  • Math.ceil(): This method rounds up a number to the nearest integer.
console.log(Math.ceil(1.5)); // 2
  • Number(): This method converts a value to a number.
console.log(Number("123")); // 123
  • Math.floor(): This method rounds down a number to the nearest integer.
console.log(Math.floor(1.5)); // 1

These are just a few of the many number methods available in JavaScript. By using these methods, you can perform a wide range of mathematical operations on numbers in your code.

Converting Variables to Numbers

JavaScript provides several methods for converting variables to numbers. Here are some of the most commonly used ones:

  • parseInt(): This method parses a string and returns an integer. It takes two optional arguments: the base (radix) of the number being parsed and the index of the first character to be parsed.
let num = parseInt("123");
console.log(num); // 123
  • parseFloat(): This method parses a string and returns a floating-point number.
let num = parseFloat("3.14");
console.log(num); // 3.14
  • Number(): This method converts a value to a number.
let num = Number("123");
console.log(num); // 123
  • unary plus operator (+): This operator converts a value to a number. It can be used as a shorthand for the Number() method.
let num = +"123";
console.log(num); // 123
info

Converting variables to numbers can result in NaN for certain values, such as unparsable strings or undefined variables.

console.log(parseInt("hello")); // NaN
console.log(Number(undefined)); // NaN

Number Object Methods

Here are some commonly used methods of the Number object in JavaScript:

  • Number.isNaN(): This method determines whether a value is NaN (Not a Number).
console.log(Number.isNaN("hello")); // false
console.log(Number.isNaN(NaN)); // true
  • Number.isFinite(): This method determines whether a value is a finite number.
console.log(Number.isFinite(123)); // true
console.log(Number.isFinite(Infinity)); // false
  • Number.isInteger(): This method determines whether a value is an integer.
console.log(Number.isInteger(5)); // true
console.log(Number.isInteger(5.1)); // false
  • Number.parseInt(): This method parses a string and returns an integer. It takes two optional arguments: the string to be parsed and the base (radix) of the number being parsed.
let num = Number.parseInt("123");
console.log(num); // 123
  • Number.parseFloat(): This method parses a string and returns a floating-point number.
let num = Number.parseFloat("3.14");
console.log(num); // 3.14
  • Number.toFixed(): This method returns a string representation of a number with a fixed number of digits after the decimal point.
let num = 3.14159;
console.log(num.toFixed(2)); // "3.14"
  • Number.toPrecision(): This method returns a string representation of a number with a specified number of total digits.
let num = 1234.5678;
console.log(num.toPrecision(4)); // "1235"
  • Number.MAX_VALUE: This property returns the largest representable number in JavaScript.
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
  • Number.MIN_VALUE: This property returns the smallest representable number in JavaScript.
console.log(Number.MIN_VALUE); // 5e-324

By using these methods, you can perform a variety of operations on numbers in JavaScript, such as checking for NaN or finite values, parsing strings as numbers, and formatting numbers for display.