A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-numbers/ below:

JavaScript Numbers - GeeksforGeeks

JavaScript Numbers

Last Updated : 11 Jul, 2025

JavaScript numbers are primitive data types, and unlike other programming languages, you don't need to declare different numeric types like int, float, etc. JavaScript numbers are always stored in double-precision 64-bit binary format IEEE 754. This format stores numbers in 64 bits:

Numeric Types in JavaScript

In JavaScript, numbers play an important role, and understanding their behavior is essential for effective programming. Let’s explore the various aspects of numeric types in JavaScript.

1. The Only Numeric Type

As we know, JavaScript has only one numeric type: the double-precision 64-bit binary format IEEE 754, which means that it doesn’t differentiate between integers and floating-point numbers explicitly. Instead, it uses a unified approach for all numeric values.

2. Scientific Notation

JavaScript allows writing extra-large or extra-small numbers using scientific (exponent) notation.

JavaScript
let a  =  156e5;
let b =   156e-5;
console.log(a);
console.log(b);
3. Integer Precision

Integers (numbers without a period or exponent notation) are accurate up to 15 digits.

JavaScript
let a = 999999999999999;
let b = 9999999999999999;
console.log(a);
console.log(b);

Output
999999999999999
10000000000000000
4. Floating Point Precision

Floating point arithmetic is not always 100% accurate due to binary representation limitations.

Example

let x = 0.22 + 0.12;   //x will be 0.33999999999999997

To solve this problem, multiply and divide:

let x = (0.22 * 10 + 0.12 * 10) / 10;    // x will be 0.34
JavaScript
let x = 0.22 + 0.12;
let y = (0.22 * 10 + 0.12 * 10) / 10;
console.log(x);
console.log(y);

Output
0.33999999999999997
0.34
5. Adding Numbers and Strings JavaScript
// Adding two numbers
let x = 10;
let y = 15;
let z = x + y;
console.log(z);

// Concatenating two strings:

let a = "10";
let b = "30";
let c  = a + b;
console.log(c);
6. Numeric Strings

JavaScript automatically converts the numeric strings to numbers in most operations like.

JavaScript
let x  = "100" / "10";
let y  = "100" * "10";
let z = "100" - "10";
console.log(x);
console.log(y);
console.log(z);
Number Literals

The types of number literals You can use decimal, binary, octal, and hexadecimal.

1. Decimal Numbers

JavaScript Numbers does not have different types of numbers(ex: int, float, long, short) which other programming languages do. It has only one type of number and it can hold both with or without decimal values.

JavaScript
let a=33;
let b=3.3;
console.log(a);
console.log(b);
2. Octal Number

If the number starts with 0 and the following digits are smaller than 8. It will be parsed as an Octal Number. 

JavaScript
let x = 0562;
console.log(x);
3. Binary Numbers

They start with 0b or 0B followed by 0's and 1's. 

JavaScript
let x = 0b11; 
let y = 0B0111;

console.log(x);
console.log(y);
4. Hexadecimal Numbers

They start with 0x or 0X followed by any digit belonging (0123456789ABCDEF) 

JavaScript
let x = 0xfff;
console.log(x);
Number Coercion in JavaScript

In JavaScript, coercion refers to the automatic or implicit conversion of values from one data type to another. When different types of operators are applied to values, JavaScript performs type coercion to ensure that the operation can proceed. Let’s explore some common examples of coercion:

1. Undefined to NaN

When you perform an operation involving undefined, JavaScript returns NaN (Not-a-Number).

JavaScript
const res = undefined + 10;
console.log(res); // NaN
2. Null to 0

The value null is coerced to 0 when used in arithmetic operations.

JavaScript
const total = null + 5;
console.log(total); // 5
3. Boolean to Number

Boolean values (true and false) are converted to numbers: 1 for true and 0 for false.

JavaScript
const n1 = true + 10;
const n2 = false + 10;

console.log(n1);
console.log(n2);
4. String to Number

When performing arithmetic operations, JavaScript converts strings to numbers. If the string cannot be parsed as a valid number, it returns NaN.

JavaScript
const s1 = '42';
const s2 = 'hello';

const strToNum1 = Number(s1);
const strToNum2 = Number(s2);

console.log(strToNum1);
console.log(strToNum2);
5. BigInts and Symbols

Attempting to coerce Symbol values to numbers results in a TypeError.

JavaScript
const symbol = Symbol('mySymbol');

const symToNum = Number(symbol); // TypeError
console.log(symToNum);

Output:

TypeError: Cannot convert a Symbol value to a number
Integer conversion

Some operations such as those which work with an array, string indexes, or date/time expect integers. After performing the coercion if the number is greater than 0 it is returned as the same and if the number NaN or -0, it is returned as 0. The result is always an integer.

Fixed-width number Conversion

In Javascript, there are some functions that deal with the binary encoding of integers such as bitwise operators and typedArray objects. The bitwise operators always convert the operands to 32-bit integers.

JavaScript Number Methods

Now, we will use Number methods such as toString(), toExponential(), toPrecision(), isInteger(), and toLocaleString() method. Let's see the examples of these Number methods.

JavaScript
let x = 21
console.log(x.toString());
console.log(x.toExponential());
console.log(x.toPrecision(4));
console.log(Number.isInteger(x));
console.log(x.toLocaleString("bn-BD"));

Output:

21
2.1e+1
21.00
true
২১
Some Facts About Numbers in JavaScript

We have a complete list of Javascript Number Objects methods, to check those please go through this Javascript Number Complete Reference article.


JavaScript Numbers | Web Development


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4