Numbers
1. Arithmetic Operations
Resources
- The following are arithmetic operators:
+
: addition-
: subtraction*
: multiplication**
: exponential/
: division%
: modulus (remainder)++
: increment (different between pre-increment (++i
) and post-increment (--i
).--
: decrement
- Operator precedence describes the order in which operations are performed in an arithmetic expression.
- Multiplication and division are done before addition and subtraction.
- Parentheses take the highest precedence.
Arithmetic operations can be performed on numbers, variables, or expressions.
// numbers
let x = 100 + 50;
console.log(x) // Output;
// variables
let x = a + b;
// expressions
let x = (100 + 50) * a;
2. JavaScript Numbers
Number is a data type in JavaScript. JavaScript has only one type of number. Numbers can be written with or without decimals.
let x = 3.14; // A number with decimals
let y = 3; // A number without decimals
console.log(typeof(x)) // Output: number
console.log(typeof(y)) // Output: number
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. → 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.
To round a number to a certain decimal place, we can use:
toFixed()
: returns a string representation of the number with the specified number of decimal places
let num = 3.14159;
let roundedNum = num.toFixed(1);
console.log(roundedNum); // Output: "3.1"
Math.round()
: rounds the number to the nearest integer and then divides by 10 to get one decimal place.
let num = 3.14159;
let roundedNum1 = Math.round(num * 10) / 10;
let roundedNum2 = Math.round(num * 100) / 100;
console.log(roundedNum1); // Output: 3.1
console.log(roundedNum2); // Output: 3.14
3. Adding Numbers and Strings - +
Operator
JavaScript uses the +
operator for both addition and concatenation (string concatenation).
// Number Addition
let x = 10, y = 20;
let z = x + y;
console.log(z); // Output: 30
// String Concatenation
let x = "10", y = "20";
let z = x + y;
console.log(z); // Output: 1020
// Adding a number and a string -> result is a string
let x = "10", y = 20;
let z = x + y;
console.log(z); // Ouput: 1020
// Adding numbers and a string
let x = 10, y = 20, z = "30";
let result = x + y + z; // Output: 3030
- If you add a number and a string, the result will be a string concatenation.
- If you add more than one number and a string, because the JavaScript interpreter works from left to right, the numbers are added first, then the string is concatenated.
Note: Operator
+
can also be used for numeric conversion.console.log(+true); // 1 → number
console.log(+””); // 0 → number
4. Numeric Strings
JavaScript strings can have numeric content:
let x = 100; // x is a number
let y = "100"; // y is a numeric string
JavaScript will try to convert strings to numbers in all numeric operations except addition:
let x = "100", y = "10";
console.log(x / y); // Output: 10
console.log(x * y); // Output: 1000
console.log(x - y); // Output: 90
console.log(x + y) // Output: 10010 - string concatenation
5. NaN - Not a Number
NaN
is a JavaScript-reserved word indicating that a number is not a legal number. NaN
is a number: typeof NaN
returns number
.
- Trying to do arithmetic with a non-numeric string will result in
NaN
(Not a Number). - If you use
NaN
in a mathematical operation, the result will also beNaN
, or the result will be a string concatenation if you’re addingNaN
with strings.
// arithmetic operations with non-numeric string
let x = 100 / "Apple"; // Output: NaN
let x = 100 / "10"; // Output: 10 -> numeric string
// Using NaN in a mathematical operation
let a = NaN, b = 5, c = "10";
let result = a + b; // Output: NaN
let d = a + c; // Output: NaN10
- You can use the global JavaScript function
isNaN()
to find out if a value is not a number:
let x = 100 / "Apple";
isNaN(x); // Output: true