Operators Flashcards
Unary, binary and tertiary operators
Unary Operators:
- Operate on a single operand.
- Example: !
(logical NOT), -
(negation), typeof
Binary Operators:
- Operate on two operands.
- Example: +
(addition), -
(subtraction), *
(multiplication), /
(division)
Ternary Operator:
- Operates on three operands.
- Often used as a shortcut for the if
statement.
- Example: condition ? expr1 : expr2
(If condition is true, evaluate expr1; otherwise, evaluate expr2)
“Expressions and operators - JavaScript | MDN” (developer.mozilla.org). Retrieved June 20, 2023.
Assignment operators
Assignment operators in
JavaScript are used to assign
values to variables.
-
=
: Assigns value to a variable
Example:x = 10;
-
+=
: Adds and assigns
Example:x += 5;
(same asx = x + 5;
) -
-=
: Subtracts and assigns
Example:x -= 5;
(same asx = x - 5;
) -
*=
: Multiplies and assigns
Example:x *= 5;
(same asx = x * 5;
) -
/=
: Divides and assigns
Example:x /= 5;
(same asx = x / 5;
) -
%=
: Modulus and assigns
Example:x %= 5;
(same asx = x % 5;
) -
**=
: Exponentiation and assigns
Example:x **= 2;
(same asx = x ** 2;
) -
<<=
: Left shift and assigns
Example:x <<= 2;
(same asx = x << 2;
) -
>>=
: Right shift and assigns
Example:x >>= 2;
(same asx = x >> 2;
) -
>>>=
: Unsigned right shift and assigns
Example:x >>>= 2;
(same asx = x >>> 2;
) -
&=
: Bitwise AND and assigns
Example:x &= 5;
(same asx = x & 5;
) -
^=
: Bitwise XOR and assigns
Example:x ^= 5;
(same asx = x ^ 5;
) -
|=
: Bitwise OR and assigns
Example:x |= 5;
(same asx = x | 5;
)
“Assignment operators” (developer.mozilla.org). Retrieved June 20, 2023.
Comparison operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Here are the main comparison operators in JavaScript:
-
==
(Equality) - checks if the values of two operands are equal or not; if yes, then the condition becomes true. May do type cohercion before checking the equality.
Example: (4 == '4'
) is true, but (4 == 5
) is false. -
===
(Strict equality) - checks if the values and types of two operands are equal or not; if yes, then the condition becomes true.
Example: (4 === 4
) is true, but (4 === '4
’) is false. -
!=
(Inequality) - checks if the values of two operands are not equal; if not, then the condition becomes true. May do type cohercion before checking the equality.
Example: (4 != 5
) is true, but (5 != '5'
) is false. -
!==
(Strict inequality) - checks if the values and types of two operands are not equal; if not, then the condition becomes true.
Example: (4 !== '4'
) is true, but (4 !== 4
) is false. -
>
(Greater than) - checks if the value of the left operand is greater than the value of the right operand; if yes, then the condition becomes true.
Example: (5 > 4
) is true, but (3 > 4
) is false. -
<
(Less than) - checks if the value of the left operand is less than the value of the right operand; if yes, then the condition becomes true.
Example: (3 < 4
) is true, but (5 < 4
) is false. -
>=
(Greater than or equal to) - checks if the value of the left operand is greater than or equal to the value of the right operand.
Example: (4 >= 4
) is true, but (3 >= 4
) is false. -
<=
(Less than or equal to) - checks if the value of the left operand is less than or equal to the value of the right operand.
Example: (3 <= 4
) is true, but (5 <= 4
) is false.
“Comparison operators” (developer.mozilla.org). Retrieved June 21, 2023.
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations on numbers.
Here are the main arithmetic operators in JavaScript:
-
+
(Addition) - adds two numbers.
Example:5 + 2
evaluates to7
. -
-
(Subtraction) - subtracts the right-hand operand from the left-hand operand.
Example:5 - 2
evaluates to3
. -
*
(Multiplication) - multiplies two numbers.
Example:5 * 2
evaluates to10
. -
/
(Division) - divides the left-hand operand by the right-hand operand.
Example:5 / 2
evaluates to2.5
. -
%
(Modulus) - returns the remainder when the left-hand operand is divided by the right-hand operand.
Example:5 % 2
evaluates to1
. -
++
(Increment) - increases the value of a variable by1
.
Example: Ifx = 5
, then++x
setsx
to6
. -
--
(Decrement) - decreases the value of a variable by1
.
Example: Ifx = 5
, then--x
setsx
to4
. -
**
(Exponentiation) - raises the left-hand operand to the power of the right-hand operand.
Example:2 ** 3
evaluates to8
.
These operators can be used with numbers or variables containing numbers to perform calculations in JavaScript.
“Arithmetic operators” (developer.mozilla.org). Retrieved June 21, 2023.
Bitwise operators
A bitwise operator treats their operands as a set of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001
. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.
Here are the main bitwise operators in JavaScript:
-
&
(Bitwise AND) - Sets each bit to1
if both bits are1
.
Example:5 & 1
evaluates to1
(0101 & 0001
). -
|
(Bitwise OR) - Sets each bit to1
if one of the two bits is1
.
Example:5 | 1
evaluates to5
(0101 | 0001
). -
^
(Bitwise XOR) - Sets each bit to1
if only one of the two bits is1
.
Example:5 ^ 1
evaluates to4
(0101 ^ 0001
). -
~
(Bitwise NOT) - Inverts all the bits.
Example:~5
evaluates to-6
(~0101
). -
<<
(Left Shift) - Shifts the left operand’s bits to the left, and fill in the right with 0s.
Example:5 << 1
evaluates to10
(0101 <<
1). -
>>
(Sign-Propagating Right Shift) - Shifts the left operand’s bits to the right, discarding bits shifted off.
Example:5 >> 1
evaluates to2
(0101 >> 1
). -
>>>
(Zero-Fill Right Shift) - Shifts the left operand’s bits to the right, discarding bits shifted off, and shifts in 0s from the left.
Example:5 >>> 1
evaluates to2
(0101 >>> 1
).
Bitwise operators are often used in low-level programming, such as working with binary data and memory manipulation. They can be powerful tools but are not as commonly used in higher-level web development tasks.
“Bitwise operators” (developer.mozilla.org). Retrieved June 21, 2023.
What are logical operators?
Logical operators are typically used with boolean (logical) values to perform logical operations. The primary logical operators in JavaScript are:
- Logical AND (
&&
) - Logical OR (
||
) - Logical NOT (
!
)
They return a boolean value when used with boolean values. However, the &&
and ||
operators actually return the value of one of the specified operands when used with non-boolean values.
“Logical operators” (developer.mozilla.org). Retrieved June 22, 2023.
What is the logical AND (&&
) operator?
The logical AND (&&
) operator returns the first operand if it can be converted to false; otherwise, it returns the second operand. So, when used with boolean values, &&
returns true
if both operands are true
; otherwise, it returns false
.
Example:
const a1 = true && true; // Returns true const a5 = "Cat" && "Dog"; // Returns "Dog" const a6 = false && "Cat"; // Returns false
“Logical operators” (developer.mozilla.org). Retrieved June 22, 2023.
What is the logical OR (||
) operator?
The Logical OR (||
) operator returns the first operand if it can be converted to true
; otherwise, it returns the second operand. So, when used with Boolean values, ||
returns true
if either operand is true
; if both are false
, it returns false
.
Example:
const o1 = true || true; // Returns true const o5 = "Cat" || "Dog"; // Returns "Cat" const o6 = false || "Cat"; // Returns "Cat"
“Logical operators” (developer.mozilla.org). Retrieved June 22, 2023.
What is the logical NOT (!
) operator?
The logical NOT (!
) operator returns false
if its single operand can be converted to true
; otherwise, it returns true
.
Example:
const n1 = !true; // Returns false const n3 = !"Cat"; // Returns false
“Logical operators” (developer.mozilla.org). Retrieved June 22, 2023.
What is short-circuit evaluation?
As logical expressions are evaluated left to right, they are tested for possible “short-circuit” evaluation using the following rules:
-
false && anything
is short-circuit evaluated tofalse
. -
true || anything
is short-circuit evaluated to true.
The rules of logic guarantee that these evaluations are always correct. Note that the "anything"
part of the above expressions is not evaluated, so any side effects of doing so do not take effect.
“Logical operators” (developer.mozilla.org). Retrieved June 22, 2023.
What is the Nullish coalescing operator (??
)?
The Nullish coalescing operator (??
) is similar to the logical OR (||
) operator, but it only returns the second expression when the first one is “nullish”, i.e., null
or undefined
. It is thus the better alternative to provide defaults when values like ''
or 0
are valid values for the first expression.
“Logical operators” (developer.mozilla.org). Retrieved June 22, 2023.
What are BigInt operators?
BigInt
operators in JavaScript operate on BigInt values, which are used to represent integers of arbitrary length. Most operators that can be used between numbers can also be used between BigInt
values, including addition (+
), subtraction (-
), multiplication (*
), and division (/
).
Example:
// BigInt addition const a = 1n + 2n; // Returns 3n
Note: Division with BigInts rounds towards zero.
“BigInt operators” (developer.mozilla.org). Retrieved June 23, 2023.
What is the exception to using operators with BigInts?
The unsigned right shift operator (>>>
) is not defined for BigInt values. This is because a BigInt does not have a fixed width, so technically it does not have a “highest bit”.
Example:
const d = 8n >>> 2n; // TypeError: BigInts have no unsigned right shift, use >> instead
“BigInt operators” (developer.mozilla.org). Retrieved June 23, 2023.
Can you mix BigInts and numbers in JavaScript calculations?
No, BigInts and numbers are not mutually replaceable. Mixing them in calculations will result in a TypeError
.
Example:
const a = 1n + 2; // TypeError: Cannot mix BigInt and other types
This is because BigInt
is neither a subset nor a superset of numbers. BigInt
s have higher precision than numbers when representing large integers, but cannot represent decimals, so implicit conversion on either side might lose precision.
“BigInt operators” (developer.mozilla.org). Retrieved June 23, 2023.
How do you explicitly convert between BigInts and numbers?
You can use explicit conversion to signal whether you want the operation to be a number operation or a BigInt
one.
Example:
const a = Number(1n) + 2; // Returns 3 const b = 1n + BigInt(2); // Returns 3n
“BigInt operators” (developer.mozilla.org). Retrieved June 23, 2023.