Advanced Operators
When operators are put together without parenthesis, the order in which they are applied is determined by the precedence of the operators. Multiplication (*)
and division (/)
has higher precedence than addition (+)
and subtraction (-)
.
// multiplication is done first, which is then followed by addition
let x = 100 + 50 * 3; // 250
// with parenthesis operations inside the parenthesis are computed first
let y = (100 + 50) * 3; // 450
// operations with the same precedences are computed from left to right
let z = 100 / 50 * 3;
Several advanced math operators can use be used while writing program. Here is a list of some of the main advanced math operators:
- Modulo operator (
%
): The modulo operator returns the remainder of a division operation. For example:
console.log(10 % 3); // 1
console.log(11 % 3); // 2
console.log(12 % 3); // 0
- Exponentiation operator (* *): The exponentiation operator raises a number to the power of another number. It is a newer operator and is not supported in all browsers, so you may need to use the
Math.pow
function instead. For example:
console.log(2 ** 3); // 8
console.log(3 ** 2); // 9
console.log(4 ** 3); // 64
- Increment operator (
++
): The increment operator increments a number by one. It can be used as a prefix (before the operand) or a postfix (after the operand). For example:
let x = 1;
x++; // x is now 2
++x; // x is now 3
- Decrement operator (
--
): The decrement operator decrements a number by one. It can be used as a prefix (before the operand) or a postfix (after the operand). For example:
let y = 3;
y--; // y is now 2
--y; // y is now 1
- Math object: The
Math
object is a built-in object in JavaScript that provides mathematical functions and constants. You can use the methods of theMath
object to perform advanced math operations, such as finding the square root of a number, calculating the sine of a number, or generating a random number. For example:
console.log(Math.sqrt(9)); // 3
console.log(Math.sin(0)); // 0
console.log(Math.random()); // a random number between 0 and 1
These are just a few examples of the advanced math operators and functions available in JavaScript. There are many more that you can use to perform advanced math operations while writing program.
let num1 = 10; let num2 = 5; // ++ operator to increment the value of num1. num1++; const result1 = num1; // 11 // -- operator to decrement the value of num2. num2--; const result2 = num2; // 4 // += operator to add num2 to num1. num1 += num2; const result3 = num1 // 15 // -= operator to subtract num2 from num1. num1 -= num2; const result4 = num1 // 11
assert(result1 === 11 && result2 === 4 && result3 === 15 && result4 === 11 );
Nullish coalescing operator '??'
The nullish
coalescing operator returns the first argument if it's not null/undefined
, else the second one. It is written as two question marks ??
. The result of x ?? y
is:
- if
x
is defined, thenx
, - if
y
isn’t defined, theny
.
It's a recent addition to the language and might need polyfills to support old browsers