This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here! Smartphone icons created by Freepik - Flaticon
4.2 JS Numbers
JavaScript has only one type of number. Numbers can be written with or without decimals.
var x = 3.14; // A number with decimals
var y = 3; // A number without decimals
Extra large or extra small numbers can be written with scientific (exponent) notation:
var x = 123e5; // 12300000
var y = 123e-5; // 0.00123
JavaScript Numbers are Always 64-bit Floating Point
Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.
JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:
Value (aka Fraction/Mantissa)
Exponent
Sign
52 bits (0 - 51)
11 bits (52 - 62)
1 bit (63)
Precision
Integers (numbers without a period or exponent notation) are accurate up to 15 digits.
var x = 999999999999999; // x will be 999999999999999
var y = 9999999999999999; // y will be 10000000000000000
The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:
var x = 0.2 + 0.1; // x will be 0.30000000000000004
To solve the problem above, it helps to multiply and divide:
var x = (0.2 * 10 + 0.1 * 10) / 10; // x will be 0.3
Adding Numbers and Strings
Warning!!
JavaScript uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
If you add two numbers, the result will be a number:
var x = 10;
var y = 20;
var z = x + y; // z will be 30 (a number)
If you add two strings, the result will be a string concatenation:
var x = "10";
var y = "20";
var z = x + y; // z will be 1020 (a string)
If you add a number and a string, the result will be a string concatenation:
var x = 10;
var y = "20";
var z = x + y; // z will be 1020 (a string)
A common mistake is to expect this result to be 30:
var x = 10;
var y = 20;
var z = "The result is: " + x + y;
A common mistake is to expect this result to be 102030:
var x = 10;
var y = 20;
var z = "30";
var result = x + y + z;
The JavaScript interpreter works from left to right.
First 10 + 20 is added because x and y are both numbers.
Then 30 + "30" is concatenated because z is a string.
Numeric Strings
JavaScript strings can have numeric content:
var x = 100; // x is a number
var y = "100"; // y is a string
JavaScript will try to convert strings to numbers in all numeric operations:
This will work:
var x = "100";
var y = "10";
var z = x / y; // z will be 10
This will also work:
var x = "100";
var y = "10";
var z = x * y; // z will be 1000
But this will not work:
var x = "100";
var y = "10";
var z = x + y; // z will not be 110 (It will be 10010)
In the last example JavaScript uses the + operator to concatenate the strings.
NaN - Not a Number
NaN is a JavaScript reserved word indicating that a number is not a legal number.
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):
var x = 100 / "Apple"; // x will be NaN (Not a Number)
However, if the string contains a numeric value , the result will be a number:
var x = 100 / "10"; // x will be 10
You can use the global JavaScript function isNaN() to find out if a value is a number:
var x = 100 / "Apple";
isNaN(x); // returns true because x is Not a Number
Watch out for NaN. If you use NaN in a mathematical operation, the result will also be NaN:
var x = NaN;
var y = 5;
var z = x + y; // z will be NaN
Or the result might be a concatenation:
var x = NaN;
var y = "5";
var z = x + y; // z will be NaN5
NaN is a number: typeof NaN returns number:
typeof NaN; // returns "number"
Infinity
Infinity (or -Infinity ) is the value JavaScript will return if you calculate a number outside the largest possible number.
var myNumber = 2;
while (myNumber != Infinity) { // Execute until Infinity
myNumber = myNumber * myNumber;
}
Division by 0 (zero) also generates Infinity:
var x = 2 / 0; // x will be Infinity
var y = -2 / 0; // y will be -Infinity
Infinity is a number: typeof Infinity returns number .
typeof Infinity; // returns "number"
Hexadecimal
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.
var x = 0xFF; // x will be 255
Never write a number with a leading zero (like 07).
Some JavaScript versions interpret numbers as octal if they are written with a leading zero.
By default, JavaScript displays numbers as base 10 decimals.
But you can use the toString() method to output numbers from base 2 to base 36.
Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Binary is base 2.
var myNumber = 32;
myNumber.toString(10); // returns 32
myNumber.toString(32); // returns 10
myNumber.toString(16); // returns 20
myNumber.toString(8); // returns 40
myNumber.toString(2); // returns 100000
Numbers Can be Objects
Normally JavaScript numbers are primitive values created from literals:
var x = 123;
But numbers can also be defined as objects with the keyword new:
var y = new Number(123);
var x = 123;
var y = new Number(123);
// typeof x returns number
// typeof y returns object
Do not create Number objects. It slows down execution speed.
The new keyword complicates the code. This can produce some unexpected results:
When using the == operator, equal numbers are equal:
var x = 500;
var y = new Number(500);
// (x == y) is true because x and y have equal values
When using the === operator, equal numbers are not equal, because the === operator expects equality in both type and value.
var x = 500;
var y = new Number(500);
// (x === y) is false because x and y have different types
Or even worse. Objects cannot be compared:
var x = new Number(500);
var y = new Number(500);
// (x == y) is false because objects cannot be compared
Note the difference between (x==y) and (x===y) .
Comparing two JavaScript objects will always return false.
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Strings part 1
JS Strings part 2
JS Strings part 3
JS Strings length Property
JS Strings the escape sequence \"
JS Strings the escape sequence \'
JS Strings the escape sequence \\
JS Statements
JS Strings line break
JS Strings line break +
JS Strings line break NOT
JS Strings typeof
JS Strings - equal value
JS Strings - not equal type
JS Strings - cannot compare Objects
JS Strings - Objects cannot be compared
JS Strings slice()
JS Strings The slice() Method part 1
JS Strings The slice() Method part 2
JS Strings The slice() Method part 3
JS Strings the substring() method
JS Strings the substr() method part 1
JS Strings the substr() method part 2
JS Strings the substr() method part 3
JS Strings the replace() method part 1
JS Strings the replace() method part 2
JS Strings the replace() method part 3
Mobile test of using JS CSS
JS Strings the replace() method part 4
JS Strings the replace() method part 5
JS Strings Convert string part 1
JS Strings Convert string part 2
JS Strings Convert string part 3
JS Strings Convert string part 4
JS Strings The padStart() Method part 1
JS Strings The padStart() Method part 2
JS Strings The padStart() Method part 3
JS Strings The padEnd() Method part 1
JS Strings The padEnd() Method part 2
JS Strings The padEnd() Method part 3
JS Strings The charAt() Method
JS Strings The charCodeAt() Method
JS Strings Property Access part 1
JS Strings Property access part 2
JS String Methods String split() part 1
JS String Methods String split() part 2
JS String The indexOf() Method
JS Numbers part 1
JS Numbers part 2
JS Numbers part 3
Floating Point Precision part 1
Floating Point Precision part 2
Numbers and Strings part 1
Numbers and Strings part 2
Numbers and Strings part 3
Numbers and Strings part 4
Numbers and Strings part 5
Numbers and Strings part 6
Numbers and Strings part 7
Numbers and Strings part 8
JS Numbers, NaN (Not a Number) part 1
JS Numbers, NaN (Not a Number) part 2
JS Numbers, NaN (Not a Number) part 3
JS Numbers, NaN (Not a Number) part 4
JS Numbers, NaN (Not a Number) part 5
JS Numbers, NaN (Not a Number) part 6
JS Numbers Infinity part 1
JS Numbers Infinity part 2
JS Numbers Infinity part 3
JS Numbers - Hexadecimal part 1
JS Numbers - Hexadecimal part 2
JS Numbers As Objects part 1
JS Numbers As Objects part 2
JS Numbers As Objects part 3
JS Numbers As Objects part 4
JS Number Methods part 1
JS Number Methods part 2
JS Number Methods part 3
JS Number Methods part 4
JS Number Methods part 5
JS Number Methods part 6
JS Number Methods part 7
JS Number Methods part 8
JS Number Methods part 9
JS Number Methods MAX_VALUE Property
JS Number Methods MIN_VALUE Property
JS Number Methods POSITIVE_INFINITY Property part 1
JS Number Methods POSITIVE_INFINITY Property part 2
JS Number Methods NEGATIVE_INFINITY Property part 1
JS Number Methods NEGATIVE_INFINITY Property part 2
JS Number Methods NaN part 1
JS Number Methods NaN part 2
JS Number Methods Number Properties
JS Arrays part 1
JS Arrays part 2
JS Arrays part 3
JS Arrays part 4
JS Arrays part 5
JS Arrays part 6
JS Arrays part 7
JS Arrays part 8
JS Arrays part 9
JS Arrays part 10
JS Arrays part 11
JS Arrays part 12
JS Arrays part 13
JS Arrays part 14
JS Arrays part 15
JS Arrays part 16
JS Arrays part 17
JS Arrays part 18
JS Arrays part 19
JS Arrays part 20
JS Arrays part 21
JS Arrays isArray() Method
JS Arrays isArray() function
JS Arrays The instanceof Operator
JS Arrays The toString() Method
JS Arrays The join() Method
JS Arrays The pop() Method part 1
JS Arrays The pop() Method part 2
JS Arrays The push() Method part 1
JS Arrays The push() Method part 2
JS Arrays The shift() Method part 1
JS Arrays The shift() Method part 2
JS Arrays The shift() Method part 3
JS Arrays The shift() Method part 4
JS Arrays Bracket Indexing
JS Arrays The length Property
JS Arrays The delete Method
JS Arrays The splice() Method part 1
JS Arrays The splice() Method part 2
JS Arrays The splice() Method part 3
JS Arrays The concat() Method part 1
JS Arrays The concat() Method part 2
JS Arrays The concat() Method part 3
JS Arrays The slice() Method part 1
JS Arrays The slice() Method part 2
JS Arrays The slice() Method part 3
JS Arrays The slice() Method part 4
JS Arrays The toString() Method part 1
JS Arrays The toString() Method part 2
JS Arrays The sort() Method part 1
JS Arrays Sort in Reverse
JS Arrays The sort() Method part 2
JS Arrays The sort() Method part 3
JS Arrays The sort() Method part 4
JS Arrays The sort() Method part 5
JS Arrays The sort() Method part 6
JS Arrays The sort() Method part 7
JS Arrays The sort() Method part 8
JS Array Sort The Highest Number
JS Array Sort The lowest number
JS Arrays sort the highest number
JS Arrays sort the lowest number
JS Arrays sort car objects by age
JS Arrays sort car objects by type
JS Arrays The forEach() Method part 1
JS Arrays The forEach() Method part 2
JS Arrays The map() Method part 1
JS Arrays The map() Method part 2
JS Arrays The filter() Method part 1
JS Arrays The filter() Method part 2
JS Arrays The reduce() Method part 1
JS Arrays The reduce() Method part 2
JS Arrays The reduce() Method part 3
JS Arrays The reduceRight() Method part 1
JS Arrays The reduceRight() Method part 2