When we talk about numbers in JavaScript, we refer to positive and negative integers, floats, binary, octal, hexadecimal, and exponential values. And JavaScript allows you to do a lot with these numbers.

Overview

Notably, integer values will be accurate up to 15 digits; if they go past 16 digits and beyond, they will be changed and rounded up or down. Again, using BigInt is the best choice for integers with more than 15 digits.

Furthermore, numbers use a double-precision 64-bit binary format, just like double in C# and Java. Contrary to many other programming languages, JavaScript does not define various types of numbers, including integers, short, long, floating-point, etc.

Again, JavaScript only uses double-precision floating point numbers for numbers. It adheres to the IEEE 754 global standard.

Additionally, the first character of a number type must be an integer value, and you cannot enclose it in quotation marks. Continue reading to learn more about JavaScript number types and usage examples in the real world.

Integer Numbers

In JavaScript, integers follow the following number pattern:

Octal (base 8)
Hexadecimal (based on 16).

Note: JavaScript treats octal and hexadecimal numbers as decimals when performing mathematical operations.

In JavaScript, we use var, const, or let to declare an integer.

let int_example = 10090;
var int_example1 = 130;
const int_example2 = 1200;

Octal numbers

Since an octal literal number in Javascript begins with the digit zero (0) and a string, it is simple to identify. Again, the JavaScript engine ignores the 0 and treats the octal number as a decimal if an octal number contains a number that is not in the range of 0 to 7. 

For example:

let num1 = 080;
console.log(num1);

#Output:
80

Hexadecimal numbers

The starting value of a hexadecimal number in javascript must be 0x or 0X, and it can have any number of hexadecimal digits (0 through 9 and A through F).

// this is a hexadecimal number example

let num_hex = 0x2a;
console.log(num_hex); // output is 42

Floating-point numbers

There must be a number after the decimal point to define a floating-point literal number in JavaScript.

// Float number example
let float_num_example= 5.89;
let float_num_example1= 0.04;

When working with extremely large numbers, you must use the e-notation. A number with an “E” should be multiplied by 10 and raised to the corresponding power.

// example using the e-notation when the value of a number is high
//the notation 2.23e7 means that 2.23 should be multiplied by 107
let float_num_example= 2.23e7;

console.log(float_num_example); //output is 22300000

Again, any floating-point number with at least six zeros after the decimal point is automatically converted to e-notation by JavaScript. For example:

let amount = 0.00000003;
console.log(amount);// output is == 3e-8

Big Integers

The bigint type came along with ES2022. Correspondingly, Bigint is a data type you can use to store whole numbers with values greater than 253 – 1. Again, “big integer literal” refers to an integer literal that ends with the character n.

let big_int_example = 456719925474093453n;

How to Add Numbers and Strings in JavaScript: What You Need to Know About the + Operator

The + operator is used to concatenate strings, as we have seen in previous blogs. When you want to add two numbers together in JavaScript, the operator between them performs the addition operation.

let x_ops = 59;
let y_ops = 23;
let z_sm = x_ops + x_ops;

However, a string concatenation will happen if you place this operator between two strings:

let x_strng = "58";
let y_strng = "28";
let z_sm = x_strng + y_strng;

When using integers, strings, and some operators in javascript, there is one trick you might encounter. An int or string value can perform mathematical operations using operators like subtraction, division, and multiplication, except for addition.

For example:

// int and string multiplication
let x_x = "108";
let y_y = "19";
let z_z = x_x * y_y; //output 2052
console.log(z_z)
 
// int and string subtraction
let x_x1 = "180";
let y_y1 = "104";
let z_z1 = x_x1 - y_y1; // output 76
console.log(z_z1)
// int and string division
let x_x2 = "1090";
let y_y2 = "10";
let z_z2 = x_x2 / y_y2; // output 109
console.log(z_z2)
// int and string addition
let x_x3 = "100";
let y_y3 = "10";
let z_z3 = x_x3 + y_y3; // failed (NaN)....this will give us 10010 instead of 110, which is wrong.
console.log(z_z3)

NaN – Not a Number: These are invalid numbers that JavaScript has identified as “NaN.” If a non-numeric string is used in arithmetic (let x_value = 34 / “people”;), the result will be “NaN.”

Conclusion

You learned about JavaScript numbers and how to use them in this article. To make the learning process simple, we provided examples. Ensure you practice more; you can read the documentation about JavaScript numbers at your leisure.

Categorized in: