Numbers
-
Some rules
- JavaScript Arithmetic Operators
- 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 (64-bit Floating Point) numbers.
- Integers (numbers without a period or exponent notation) are accurate up to 15 digits.
- Floating point arithmetic is not always 100% accurate. To solve the problem above, it helps to multiply and divide.
- JavaScript uses the + operator for both addition and concatenation. (Numbers are added. Strings are concatenated.)
- 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.
- If you add two numbers, the result will be a number.
- If you add two numeric strings, the result will be a concatenated string.
- If you add a number and a numeric string, the result will be a concatenated string.
- JavaScript will try to convert strings to numbers in all numeric operations.
-
let x = "100"; let y = "10"; let z = x / y;
- Output of this will be 10.
- But this won't work (the output will be 10010):
let x = "100"; let y = "10"; let z = x + y;
-
-
NaN - Not a Number
NaN
is a JavaScript reserved word indicating that a number is not a legal number.- Example:
let x = 100 / "Apple"; // output is 'NaN'
- You can use the global JavaScript function
isNaN()
to find out if a value is a not a number:let x = 100 / "Apple"; isNaN(x); // output is 'true'
- If you use
NaN
in a mathematical operation, the result will also beNaN
(or a concatenation ifNaN
is added to a number). NaN
is a number:typeof NaN
returnsnumber
.
-
Infinity
Infinity
(or-Infinity
) is the value JavaScript will return if you calculate a number outside the largest possible number.- Division by 0 (zero) also generates
Infinity
. Infinity
is a number:typeof Infinity
returnsnumber
.
-
Hexadecimal
- JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.
-
Some Methods
typeof
in JavaScript is an operator used for type checking and it returns the data type of the operand passed to it.toFixed()
method rounds the string to a specified number of decimals.// number.toFixed( value ) var test = 213.73145; console.log(test.toFixed(2)); //output is '214.73'
Number()
method converts a value to a number.// Number(value) let myNumber = "74"; myNumber = Number(myNumber) + 3; //output is '77'
unary +
converts the operand into a number if it is not a number.let apples = "2"; let oranges = "3"; // both values converted to numbers before the binary plus alert( +apples + +oranges ); // 5
-
Comma
- The comma operator allows us to evaluate several expressions, dividing them with a comma
,
. - Each of them is evaluated but only the result of the last one is returned.
let a = (1 + 2, 3 + 4); alert( a ); // 7 (the result of 3 + 4)
- The comma operator allows us to evaluate several expressions, dividing them with a comma