JavaScript has both binary and unary operators, and one special ternary operator, the conditional operator. A binary operator requires two operands, one before the operator and one after the operator:
- Ex: operand1 operator operand2
For example,
3 + 4orx * y. This form is called an infix binary operator, because the operator is placed between two operands. All binary operators in JavaScript are infix.
A unary operator requires a single operand, either before or after the operator:
- operator operand
- operand operator
For example,
x++or++x. The operator operand form is called a prefix unary operator, and the operand operator form is called a postfix unary operator. ++ and – are the only postfix operators in JavaScript — all other operators, like!,typeof, etc. are prefix. [^1]
Simple Ex: x = f()
Many compound assignment operators
Name Shorthand operator Meaning
- Assignment x = f() x = f()
- Addition assignment x += f() x = x + f()
- Subtraction assignment x -= f() x = x - f()
- Multiplication assignment x *= f() x = x * f()
- Division assignment x /= f() x = x / f()
- Remainder assignment x %= f() x = x % f()
- Exponentiation assignment x **= f() x = x ** f()
- Left shift assignment x «= f() x = x « f()
- Right shift assignment x »= f() x = x » f()
- Unsigned right shift assignment x »>= f() x = x »> f()
- Bitwise AND assignment x &= f() x = x & f()
- Bitwise XOR assignment x ^= f() x = x ^ f()
Bitwise OR assignment x = f() x = x f() - Logical AND assignment x &&= f() x && (x = f())
Logical OR assignment x = f() x (x = f()) - Nullish coalescing assignment x ??= f() x ?? (x = f())
[^1]
If an expression evaluates to an object, then the left-hand side of an assignment expression may make assignments to properties of that expression. [^1]
Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.
Operator Usage Description
- Logical AND
(&&)-expr1 && expr2Returnsexpr1if it can be converted to false; otherwise, returnsexpr2. Thus, when used with Boolean values,&&returnstrueif both operands are true; otherwise, returns false.- Logical OR
(||)-expr1 || expr2Returnsexpr1if it can be converted totrue; otherwise, returnsexpr2. Thus, when used with Boolean values,||returnstrueif either operand istrue; if both arefalse, returnsfalse.- Logical NOT
(!)-m!expr- Returnsfalseif its single operand that can be converted totrue; otherwise, returnstrue.
The following code shows examples of the && (logical AND) operator.
| The following code shows examples of the | (logical OR) operator. |
| o1 = true | true; // t | t returns true |
| const o2 = false | true; // f | t returns true |
| const o3 = true | false; // t | f returns true |
| const o4 = false | 3 === 4; // f | f returns false |
| const o5 = “Cat” | “Dog”; // t | t returns Cat |
| const o6 = false | “Cat”; // f | t returns Cat |
| const o7 = “Cat” | false; // t | f returns Cat |
The following code shows examples of the ! (logical NOT) operator.
const n1 = !true; // !t returns falseconst n2 = !false; // !f returns trueconst n3 = !"Cat"; // !t returns false“when there is a preset number of times that loop is going to run. That is a for loop. whether it’s one or a 1 million. It’s gonna stop when you want it to.” - Kassie
Structure:
for (initial value; condition must be true; increment/decrement){
code to execute
}
let students = 13;
for (let i = 0; i < 5; i++){ console.log(‘hello to student #’, i); }
“a ‘while loop’ is something you want to use when you don’t know how many times it’s going to take. So if you are trying to get input, from your user and you want to keep asking them until they give you an acceptable value. That is a while, loop. You don’t know how many times it’s going to take. Passwords are a ‘while’ loop”. - Kassie
Structure: while(this is true){execute this code}
EX:
while (5 < 10){
Console.log('infinite looooop)
}
“Expressions and operators” developer.mozilla.org
“Loops and iteration” developer.mozilla.org