that represents all the numbers including integers, floats, real, decimal as well as complex. We can either use decimal points or simple integers to represent a number.
let a = 20; // number without decimal let b = 20.1; // number with decimal
let l = 21e6; // 21*1000000 let s = 21e-6; // 21/1000000
suffix after the number represents the exponent power.
|0-52 (52 bits)||52 - 62 (11 bits )||63 (1 bit)|
let a = 999999999999999; // 999999999999999 let b = 9999999999999999; // 10000000000000000 let c = 1.999999999999999; // 1.999999999999999 let d = 1.9999999999999999; // 2
If a number contains more than 15 digits, JS will not be able to maintain the number precision.
Adding numbers and string
operator can be used to add two numbers or concatenate two strings. But if we use the + operator between a number and a string then it will convert the number into a string and concatenate them.
let a = 20; // number let b = "21"; // string let c = a + b; // 2021 string
Whenever we use the number with string along with
operator, it will always treat the number as a string and perform the concatenate operation, not addition.
let a = 20; // number let b = 20; // number let c = "Concatenate a + b " + a+b; // "Concatenate a + b 2020"
In the above example both
were numbers still the + operator performs the concatenation operation, not added because the statement before a+b was a string so the JS engine converts the complete statement as a string. Let's say you do not want to perform concatenation when both the variables are numbers and the rest of the statement is a string, there we can use the brackets, so the interpreter first executes the bracket and all of its operations then, the rest statement.
let a = 20; // number let b = 20; // number let c = "Concatenate a + b " + (a+b); //"Concatenate a + b40"
Other Arithmetic operation between string and numbers
operator performs the concatenation operation between the string and a number, by implicit conversion of the number to a string. For the rest of the JS arithmetic operators, the JS interpreter converts the string into a corresponding integer and performs the arithmetic operation.
let a = 20; //number let b ="2"; // string let mul = a*b; //40 (number) let div = a/b; //10 (number) let modu = a%b; // 0 (number) let sub = a-b; // 18 (number)
Even if we apply the arithmetic operators (except +) between two string numeric values, first both the string values will be converted into numbers and computed according to the operation.
let a = "20"; //string let b ="2"; // string let mul = a*b; //40 (number) let div = a/b; //10 (number) let modu = a%b; // 0 (number) let sub = a-b; // 18 (number)
let a = 20; // number let b = "twenty"; // string let c = a*b; // NaN
Unlike other programming languages, JS does not throw an
when we perform an arithmetic operation between a number and string or string and string, instead, it returns a new value NaN representing Not a Number.
let a = "one"; // string let b = "two"; // string let c = a*b; //NaN
keyword to represent an infinite number. The Infinite number can be positive
let a = Infinity; let b = 20; let c = a+b; // Infinity
let a = 20; let b = 0; let c = a/b; // Infinity
prefix followed by the HexaDecimal representation of a number.
let hex = 0xAB; // 171
object, which can also be used to represent the Number data type.
let p_num = 200; // 200 primitive data type let o_num = new Number(200); // 200 Object number
In the above example both
represent the same value, and both can perform most of the similar tasks. But it always suggests using the primitive number data type unless you know why you are using the
- Numbers are used to representing numeric values.
- Number support all the arithmetic operations.
- + operator performs the contamination operation between string-number and string-string.
- NaN is a Not a Number value that represents illegal numbers.
- Infinity and -Infinity represent the largest and smallest numbers.
- Number() objects can also be used to represent numbers.