Back to JavaScript
2026-02-077 min read

Number

Learn Number step by step with clear examples and exercises.

Why This Matters

Understanding the nuances of numbers in JavaScript is essential for writing efficient and bug-free code. Numbers are the foundation of any programming language, and mastering their properties, methods, and common pitfalls will help you create robust applications. Additionally, a strong command of numbers can make a difference during interviews and real-world coding challenges.

In this expanded lesson, we'll delve deeper into JavaScript numbers by covering:

  1. The inner workings of number representation in JavaScript.
  2. A comprehensive exploration of various number properties and methods for performing calculations, conversions, and validations.
  3. Common mistakes to avoid when dealing with numbers in JavaScript.
  4. Practice questions to reinforce your understanding of the concepts covered.
  5. Answers to the practice questions.
  6. Frequently asked questions about working with numbers in JavaScript.

Prerequisites

Before diving into the core concept of JavaScript numbers, we assume you have a basic understanding of:

  1. JavaScript syntax and variables
  2. Basic data types such as strings and booleans
  3. Control structures like loops and conditional statements
  4. Understanding the difference between primitive values (strings, numbers, booleans) and objects in JavaScript.
  5. Familiarity with the Math object and its methods.

If you're not familiar with these concepts, we recommend brushing up on them before proceeding with this lesson.

Core Concept

Number Representation in JavaScript

JavaScript stores all numbers using the double-precision 64-bit binary format defined by the IEEE 754 standard. This allows for representing both integers and floating-point numbers within a wide range.

Number Literals

JavaScript supports multiple ways to represent number literals for flexibility and readability:

  1. Decimal (base-10): Standard numbers, such as 10 or 3.14.
  2. Binary (base-2): Numbers prefixed with 0b, like 0b1010 or 0b11001010.
  3. Octal (base-8): Numbers prefixed with 0o, such as 0o12 or 0o77.
  4. Hexadecimal (base-16): Numbers prefixed with 0x, for example, 0xA or 0xFF.

Number Properties and Methods

JavaScript provides a wealth of properties and methods to work with numbers. In this lesson, we'll focus on the most important ones:

  1. Number(object): JavaScript's Number constructor is used to convert any type of JavaScript variable into a number format. For example:
let a = true ;
let value = Number ( a );
console . log ( value ); // Output: 1
  1. Number Properties
  • Number.EPSILON: Represents the difference between 1 and the smallest floating-point number greater than 1, which is approximately 2.2204e-16.
  • Number.MAX_SAFE_INTEGER: The maximum safe integer value in JavaScript, which is 9007199254740991.
  • Number.MAX_VALUE: The maximum numeric value that JavaScript can handle, which is approximately 1.79769313486231e+308.
  • Number.MIN_SAFE_INTEGER: The minimum safe integer value in JavaScript, which is -9007199254740991.
  • Number.MIN_VALUE: The smallest positive numeric value greater than zero, which is approximately 5e-324.
  • Number.NaN: Represents a value that is not a valid number.
  • Number.NEGATIVE_INFINITY: The largest negative value, known as negative infinity.
  • Number.POSITIVE_INFINITY: The largest positive value, known as positive infinity.
  1. Number Methods
  • Number.isNaN(value): Determines whether the passed value is NaN and of the type Number.
  • Number.isFinite(value): Checks whether the passed value is a finite number, i.e., not Infinity or NaN.
  • Number.isInteger(value): Returns true if the passed value is an integer; otherwise, it returns false.
  • Number.parseFloat(string): Accepts a string and converts it into a floating-point number.
  • Number.parseInt(string, radix): Accepts a string and converts it into an integer value with optional radix (base).

Working with Numbers in JavaScript

Now that we've covered the basics of number representation and properties/methods, let's explore some practical examples of using numbers in JavaScript:

// Converting variables to numbers
let str = "10";
let bool = false;
console.log(Number(str)); // Output: 10
console.log(Number(bool)); // Output: 0

// Using Number properties and methods
console.log(Number.MAX_SAFE_INTEGER); // Output: 9007199254740991
console.log(Number.MIN_SAFE_INTEGER); // Output: -9007199254740991
console.log(Number.isInteger(10)); // Output: true
console.log(Number.parseFloat("3.14159")); // Output: 3.14159

Common Mistakes

  1. Forgetting to convert non-numeric values: When performing arithmetic operations, always ensure that both operands are numbers. If not, you may encounter unexpected results or errors.
  2. Ignoring the difference between integers and floating-point numbers: Be aware of the differences in precision and range when working with integers and floating-point numbers.
  3. Using parseFloat() and parseInt() improperly: Ensure that you provide the correct string and radix (base) arguments to these methods for accurate conversion results.
  4. Confusing Infinity, NaN, and undefined: Understand the differences between Infinity, NaN, and undefined, as they can lead to confusing errors in your code.

Common Mistakes - Additional Examples

  1. Rounding Errors: Due to the floating-point representation of numbers in JavaScript, rounding errors may occur when performing complex calculations involving very large or small numbers. For example:
let a = 0.1 + 0.2;
console.log(a); // Output: 0.30000000000000004

In this case, the sum of 0.1 and 0.2 does not equal 0.3, as a result of rounding errors in JavaScript's floating-point representation.

  1. Incorrectly using Number(): Be careful when using the Number() constructor, as it can lead to unexpected results if used with non-numeric values:
let str = "Hello";
console.log(Number(str)); // Output: NaN

Worked Example

Calculating the Factorial of a Number

In this example, we'll create a function to calculate the factorial of a given number using recursion and JavaScript's Math.floor() method for integer division.

function factorial(n) {
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
console.log(factorial(5)); // Output: 120

FAQ

What is the difference between parseFloat() and parseInt()?

parseFloat() converts a string to a floating-point number, while parseInt() converts a string to an integer. Both functions accept an optional radix parameter to specify the base of the input string.

How can I check if a value is a valid number in JavaScript?

You can use the Number.isFinite() function to determine whether a value is a finite number (not Infinity, NaN, or undefined). However, this will not work for non-numeric values like strings and booleans. To check if a value is a valid number in all cases, you can use the following code:

function isValidNumber(value) {
return !isNaN(value) && typeof value === "number";
}

Why does JavaScript have rounding errors when working with floating-point numbers?

JavaScript uses a binary representation for floating-point numbers, which can lead to inaccuracies when performing calculations. This is due to the finite precision of the underlying data type. To minimize these issues, it's best to perform calculations using integers whenever possible and avoid relying on exact floating-point arithmetic.

Practice Questions

  1. Write a JavaScript function that checks whether a given value is an integer or not.
function isInteger(value) {
return Number.isInteger(value) || Math.floor(value) === value;
}
  1. Convert the decimal number 375 into binary format using JavaScript.
let binary = (decimal) => {
let result = [];
while (decimal > 0) {
result.unshift(decimal % 2);
decimal = Math.floor(decimal / 2);
}
return result.join("");
};
console.log(binary(375)); // Output: "1001101101"
  1. Write a script that calculates the factorial of a number using the Number constructor and multiplication operator.
function factorial(n) {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
console.log(factorial(5)); // Output: 120
  1. Given a string containing mixed numbers, create a function to separate integers and floating-point numbers in the string.
function extractNumbers(str) {
let regex = /(-?\d+(\.\d+)?)/g;
let match;
const numbers = [];
while ((match = regex.exec(str)) !== null) {
numbers.push(parseFloat(match[0]));
}
return numbers;
}
let mixedNumbers = "10, 3.14, -5, 4e-2";
console.log(extractNumbers(mixedNumbers)); // Output: [10, 3.14, -5, 0.04]

FAQ

What is the difference between parseFloat() and parseInt()?

parseFloat() converts a string to a floating-point number, while parseInt() converts a string to an integer. Both functions accept an optional radix parameter to specify the base of the input string.

How can I check if a value is a valid number in JavaScript?

You can use the Number.isFinite() function to determine whether a value is a finite number (not Infinity, NaN, or undefined). However, this will not work for non-numeric values like strings and booleans. To check if a value is a valid number in all cases, you can use the following code:

function isValidNumber(value) {
return !isNaN(value) && typeof value === "number";
}

Why does JavaScript have rounding errors when working with floating-point numbers?

JavaScript uses a binary representation for floating-point numbers, which can lead to inaccuracies when performing calculations. This is due to the finite precision of the underlying data type. To minimize these issues, it's best to perform calculations using integers whenever possible and avoid relying on exact floating-point arithmetic.

Number | JavaScript | XQA Learn