JavaScript Numbers

The JavaScript Number is a primitive data type, which allows both integers and floating-point values.

  • The Number allows both integer and floating-point values.
  • Unlike other programming languages, JavaScript doesn't treat integers and floating-point numbers differently.
  • The Integer numbers can be represented in decimal, hexadecimal, or octal notation.
  • The exponential notation can be used to represent too large or too small numbers.

Creating Numbers

The below example shows the basic use of numbers.

const a = 3;       // Integer value
const b = 3.15     // Floating-point value
const e1 = 3.15e9;    // Positive Exponential value
const e2 = 3.15e-5;   // Negative Exponential value
const h1 = 0xff;   // Hexadecimal value for 255
const h2 = 0x00 ;  // Hexadecimal value for 0

console.log(a);     // Prints: 3
console.log(b);     // Prints: 3.15
console.log(e1);    // Prints: 3150000000
console.log(e2);    // Prints: 0.0000315
console.log(h1);    // Prints: 255
console.log(h2);    // Prints: 0

+ Operator with Numbers

When the + operator is used with numbers, they are added.

const a = 7 + 9;
console.log(a);    // Prints: 16

When the + operator is used with numbers and strings, they are concatenated.

const a = '7' + 9;
console.log(a);    // Prints: 79

When other numeric operations (other than the + operator) are performed on numeric strings and numeric numbers, the numeric strings are first converted to numbers, and then operations are performed.

const a = '9' - 3;
const b = '9' / 3;
const c = '9' * 3;

console.log(a);    // Prints: 6
console.log(b);    // Prints: 3
console.log(c);    // Prints: 27

JavaScript NaN

In JavaScript, the keyword NaN stands for "Not a Number" which indicates the value is not a number.

  • Performing arithmetic operations (other than +) with numbers and strings returns NaN.
  • The built-in function isNaN can be used to find if a value is a number.
  • The data type of NaN is a number, which can be verified using the typeof operator.
const a = 5 - 'hello';
const b = 5 * 'hello';
const c = 5 / 'hello';

console.log(a);    // Prints: NaN
console.log(b);    // Prints: NaN
console.log(c);    // Prints: NaN

console.log(isNaN(5));    // Prints: false
console.log(isNaN(a));    // Prints: true

console.log(typeof a);    // Prints: number

JavaScript Infinity

In JavaScript, the arithmetic calculations that return a value that exceeds the largest (or smallest) possible number, then Infinity or -Infinity is returned.

  • In most cases, this situation occurs due to division by zero.
const a = 4 / 0;
const b = -4 / 0;

console.log(a);    // Prints: Infinity
console.log(b);    // Prints: -Infinity

Precision Problems

In JavaScript, the operations on floating-point numbers result in some unexpected numbers as shown below.

The numbers (both integers and floating-point) are stored in a binary form internally. The decimal digits of a floating-point number cannot be represented in a binary form exactly, but much closer to the actual value. This creates precision problems.

To solve this issue, we can use the method toFixed() to specify the decimal places of the result.

const a = 0.1 + 0.2;
console.log(a);    // Prints: 0.30000000000000004

console.log(a.toFixed(2));    // Prints: 0.30

In JavaScript, the integers with more than 15 digits are not accurate.

const a = 999999999999999
const b = 9999999999999999

console.log(a);    // Prints: 999999999999999
console.log(b);    // Prints: 10000000000000000

Number Object

We can also create a number using the new keyword.

However, it is recommended to avoid using the object, as it slows down the program execution

const a = 25;
const b = new Number(35);

console.log(a);    // Prints: 25
console.log(b);    // Prints: 35

console.log(typeof a);    // Prints: number
console.log(typeof b);    // Prints: object

For more details about the object properties and methods, check JavaScript Number Object.

Number Function

The function Number() can be used to convert various data types to numbers.

const a = '23';    // String value
const b = true;    // Boolean value

//Converting to number
const num1 = Number(a);
const num2 = Number(b);

console.log(num1);    // Prints: 23
console.log(num2);    // Prints: 1

For more details on number conversion, check JavaScript Type Conversions.

Overall

The JavaScript Number is a primitive data type used to represent both integers and floating-point values.

Related Links