Math basics Flashcards
What are the basic math operators?
-
+
(Addition) - Adds two numbers together.6 + 9
-
-
(Subtraction) - Subtracts the right number from the left.20 - 15
-
*
(Multiplication) - Multiplies two numbers together.3 * 7
-
/
(Division) - Divides the left number by the right.10 / 5
-
%
(Remainder, sometimes called modulo) - Returns the remainder left over after you’ve divided the left number into a number of integer portions equal to the right number.8 % 3
(Returns2
which is the reminder of8/3
) -
**
(Exponent) - Raises a base number to the exponent power, that is, the base number multiplied by itself, exponent times.5 ** 2
(returns25
, which is the same as5 * 5
).
“Arithmetic operators” (developer.mozilla.org). Retrieved October 30, 2023.
What is the operator precedence in JavaScript?
Operator precedence in JavaScript is the same as is taught in math classes in school — multiply and divide are always done first, then add and subtract (the calculation is always evaluated from left to right).
If you want to override operator precedence, you can put parentheses around the parts that you want to be explicitly dealt with first. For example:
(num2 + num1) / (8 + 2);
“Operator precedence” (developer.mozilla.org). Retrieved October 30, 2023.
Explain the increment (++
) operator
The increment (++
) operator increments (adds one to) its operand and returns the value before or after the increment, depending on where the operator is placed.
et x = 3; const y = x++; console.log(`x:${x}, y:${y}`); // Expected output: "x:4, y:3" let a = 3; const b = ++a; console.log(`a:${a}, b:${b}`); // Expected output: "a:4, b:4"
Syntax
x++ \++x
The ++
operator is overloaded for two types of operands: number
and BigInt
. It first coerces the operand to a numeric value and tests the type of it. It performs BigInt
increment if the operand becomes a BigInt
; otherwise, it performs number
increment.
If used postfix, with operator after operand (for example, x++
), the increment operator increments and returns the value before incrementing.
If used prefix, with operator before operand (for example, ++x
), the increment operator increments and returns the value after incrementing.
The increment operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets). ++x
itself evaluates to a value, not a reference, so you cannot chain multiple increment operators together.
\++(++x); // SyntaxError: Invalid left-hand side expression in prefix operation
“Increment (++) - JavaScript | MDN” (developer.mozilla.org). Retrieved October 30, 2023.
What is the value of the resulting variables in the following code?
let x = 3; const y = ++x; let x2 = 3n; const y2 = ++x2;
x is 4; y is 4
x2 is 4n; y2 is 4n
What is the value of the resulting variables in the following code?
let x = 3; const y = x++; let x2 = 3n; const y2 = x2++;
x is 4; y is 3
x2 is 4n; y2 is 3n
Explain the decrement (--
) operator
The decrement (--
) operator decrements (subtracts one from) its operand and returns the value before or after the decrement, depending on where the operator is placed.
let x = 3; const y = x--; console.log(`x:${x}, y:${y}`); // Expected output: "x:2, y:3" let a = 3; const b = --a; console.log(`a:${a}, b:${b}`); // Expected output: "a:2, b:2"
Syntax
x-- --x
The --
operator is overloaded for two types of operands: number
and BigInt
. It first coerces the operand to a numeric value and tests the type of it. It performs BigInt
decrement if the operand becomes a BigInt;
otherwise, it performs number
decrement.
If used postfix, with operator after operand (for example, x--
), the decrement operator decrements and returns the value before decrementing.
If used prefix, with operator before operand (for example, --x
), the decrement operator decrements and returns the value after decrementing.
The decrement operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets). --x
itself evaluates to a value, not a reference, so you cannot chain multiple decrement operators together.
--(--x); // SyntaxError: Invalid left-hand side expression in prefix operation
“Decrement (–) - JavaScript | MDN” (developer.mozilla.org). Retrieved October 30, 2023.
What is the value of the resulting variables in the following code?
let x = 3; const y = x--; let x2 = 3n; const y2 = x2--;
x is 2; y is 3
x2 is 2n; y2 is 3n
What is the value of the resulting variables in the following code?
let x = 3; const y = --x; let x2 = 3n; const y2 = --x2;
x is 2; y = 2
x2 is 2n; y2 is 2n
Please list and explain all assignment operators
-
+=
(Addition assignment) - Adds the value on the right to the variable value on the left, then returns the new variable value. Example:x += 4;
is equivalent tox = x + 4;
. -
-=
(Subtraction assignment) - Subtracts the value on the right from the variable value on the left, and returns the new variable value. Example:x -= 3;
is equivalent tox = x - 3;
. -
*=
(Multiplication assignment( - Multiplies the variable value on the left by the value on the right, and returns the new variable value. Examplex *= 3;
is equivalent tox = x * 3;
. -
/=
(Division assignment) - Divides the variable value on the left by the value on the right, and returns the new variable value. Examplex /= 5;
is equivalent tox = x / 5;
“Assignment operators” (developer.mozilla.org). Retrieved October 31, 2023.
Please list an explain all comparison operators
-
===
(Strict equality) - Tests whether the left and right values are identical to one another. Example:5 === 2 + 4
-
!==
(Strict-non-equality) - Tests whether the left and right values are not identical to one another. Example:5 !== 2 + 3
-
<
(Less than) - Tests whether the left value is smaller than the right one. Example:10 < 6
-
>
(Greater than) - Tests whether the left value is greater than the right one. Example:10 > 20
-
<=
(Less than or equal to) - Tests whether the left value is smaller than or equal to the right one. Example:3 <= 2
-
>=
(Greater than or equal to) - Tests whether the left value is greater than or equal to the right one.5 >= 4
“Comparison operators” (developer.mozilla.org). Retrieved November 2, 2023.
What is the Math
namespace?
The Math
namespace object contains static properties and methods for mathematical constants and functions.
Imprtant: Math
works with the Number
type. It doesn’t work with BigInt
.
“Math - JavaScript | MDN” (developer.mozilla.org). Retrieved November 2, 2023.
What does Math.abs(x)
do?
Returns the absolute value of x
.
“Math.abs() - JavaScript | MDN” (developer.mozilla.org). Retrieved November 2, 2023.
What does Math.ceil(x)
do?
Returns the smallest integer greater than or equal to x
.
“Math.ceil() - JavaScript | MDN” (developer.mozilla.org). Retrieved November 2, 2023.
What does Math.floor(x)
do?
Returns the largest integer less than or equal to x
.
“Math.floor() - JavaScript | MDN” (developer.mozilla.org). Retrieved November 2, 2023.
What does Math.max(x1, x2, ...)
do?
Returns the largest of the numbers given as input parameters.
Returns NaN
if any of the parameters is or is converted into NaN
. Returns -Infinity if no parameters are provided.
“Math.max() - JavaScript | MDN” (developer.mozilla.org). Retrieved November 2, 2023.