That first link is to the MDN reference docs, but I later found the guide to expressions and operators which presents them a bit different and with more explaining of syntax.
- unary operators require a single operand, before or after the operator
- binary operators require two operands, one before and one after the operator
- and the conditional ternary operator is the only JS operator that takes three operands
Some operators are listed under different categories in these two MDN texts, but I guess that just means that the categories are not that clear cut.
! is a unary operator, but also a logical operator.
++ is unary, but will sometimes be listed under ‘increment and decrement’.
let counter = 0;
// Unary operator with one operand (either before or after)
// Binary operator with two operands (one before and one after)
counter > 10;
assigns a value to its left operand based on the value of its right operand
The simple assignment operator is the humble
= as in the syntax that assigns a value to a variable, but there’s also a long list of compound assignment operators that are shorthand. Three examples:
Chaining assignments or nesting assignments in other expressions can result in surprising behavior. For this reason, some JS style guides discourage chaining or nesting assignments
Binary operators that take two operands: numbers, strings, logical or object values — and returns a logical value. These operators are all pretty clear to me from before. Also; coding in C for CS50 last year has built a good understanding of types and therefor strict equality. 💃🏻
That said, I think I’ve read the word logical in some contexts without it necessarily being clear to me that means we are talking boolean and
false. “Comparison operators return a logical value” is a sentence that is useful for me to properly comprehend.
- Standard arithmetic operations
%is a binary operator that returns the remainder of dividing it’s two operands
--can be used postfix or prefix, changing if the value is returned before or after one is added or subtracted from the operand.
I didn’t know that increment/decrement could be used postfix. And while I have used
% quite a bit, that specific description of it has not made sense to be before now, so that’s cool.
Yeah… I kinda understand these (on some level), but also that this is not where I need to dwell.
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.
- Logical AND
&&is used as
expr1 && expr2
- Logical OR
||is used as
expr1 || expr2
- Logical NOT
falseif the operand can be converted to
they are tested for short-circuit evaluation using the following rules:
false && anythingis short-circuit evaluated to
true || anythingis short-circuit evaluated to
+is the concatenation operator
+=can also be used to concatenate strings, and is a shorthand assignment operator
To be continued…!