javascript-tipo-number

The Number Type in JavaScript

  • 5 min

In JavaScript, the Number data type allows us to handle numeric values and perform mathematical operations with them.

Unlike other languages that have different types for integers and floating-point numbers, JavaScript uses a single type for both cases. This simplifies number manipulation (but also introduces certain problems, which we’ll see at the end).

Declaring Number Type Variables

To declare a variable with a numeric value, we simply use the keyword let or const, followed by the variable name and the value we want to assign. For example:

let age = 25;
const pi = 3.14159;

Copied!

In this case,

  • The variable age has been declared as an integer
  • The constant pi as a decimal number

As we said, in JavaScript there is no difference between creating a number with or without decimals

It is also possible to use scientific notation, when working with very large or very small numbers.

const large = 1.5e10; // 15,000,000,000
const small = 2.5e-3; // 0.0025
Copied!

Arithmetic Operations with Number Type

We can perform arithmetic operations with Number type variables using the basic mathematical operators.

let a = 10;
let b = 5;
let c = a + b; // c = 15
let d = a - b; // d = 5
let e = a * b; // e = 50
let f = a / b; // f = 2
let g = a % b; // g = 0
Copied!

We can also use parentheses to group operations and establish the order of precedence. For example:

let result = (5 + 10) * 2; // result = 30
Copied!

JavaScript also has increment (++) and decrement (--) operators, which are useful for manipulating numbers quickly:

let x = 5;
x++;  // Now x is 6
x--;  // Now x is back to 5
Copied!

Comparisons and Sorting

Numbers can be compared using the standard comparison operators:

let x = 5;
let y = 10;

console.log(x > y);   // false
console.log(x < y);   // true
console.log(x === y); // false
console.log(x !== y); // true
Copied!

We must consider possible precision problems. We’ll see this below 👇

Allowed Value Range

The Number type can handle a very large range of values, though not infinite. The maximum value that can be represented is Number.MAX_VALUE, which is approximately 1.79e+308.

If this value is exceeded, the value Infinity is obtained:

console.log(Number.MAX_VALUE);  // 1.7976931348623157e+308
console.log(1e309);             // Infinity
Copied!

Similarly, the smallest value that can be represented is Number.MIN_VALUE, which is approximately 5e-324.

console.log(Number.MIN_VALUE);  // 5e-324
console.log(-1e309);            // -Infinity
Copied!

NaN and Infinity

In addition to the usual numeric values, Javascript also has two special values that belong to the Number type: NaN and Infinity.

NaN: means “Not a Number”. It is the result of an arithmetic operation that doesn’t make sense, such as division by zero, or converting a text string that cannot be interpreted as a number.

let number = "text" * 2; // NaN
Copied!

Infinity: represents an infinite numeric value, positive or negative. It can be obtained by dividing any number by zero or by exceeding the limits of the Number type.

let inf = 1 / 0; // Infinity
let minusInf = -1 / 0; // -Infinity
Copied!

Data Type Conversion

Implicit Conversion

JavaScript performs implicit conversions between types when using operators that require specific types. For example, adding a number and a string converts the number to a string:

let number = 5;
let text = " apples";

let result = number + text; // "5 apples"
Copied!

Explicit Conversion

You can convert values explicitly using the functions Number(), String(), parseInt(), and parseFloat():

let stringNumber = "42";

let number = Number(stringNumber); // 42
let floatNumber = parseFloat("3.14"); // 3.14
let intNumber = parseInt("101", 2); // 5 (in base 2)
Copied!

Methods for Manipulating Number

JavaScript provides many methods for working with Numbers.

Bit Representation Advanced

Numeric values in JavaScript are based on the IEEE 754 standard, meaning all numbers are represented in 64-bit double-precision format (Double Precision Floating Point).

The Number type uses 64 bits in memory, of which:

  • 1 bit is for the sign (positive or negative),
  • 11 bits are for the exponent,
  • 52 bits are for the mantissa (the significant part of the number).