When MDN documents JavaScript language expressions and operators there are 70 on the list. I find they range from the extremes of kinda obvious — to lol not gonna need anytime soon. This post is my study notes of the stuff between what is currently obvious/irrelevant to me. For many of these operators; I know what they do, but I want to learn the correct names and know the categories, what belongs where.

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)
counter++;

// Binary operator with two operands (one before and one after)
counter > 10;

Assignment operators

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:

Addition assignment a += b a = a + b
Subtraction assignment a -= b a = a - b
Multiplication assignment a *= b a = a * b

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

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

Comparison operators

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 true / false. “Comparison operators return a logical value” is a sentence that is useful for me to properly comprehend.

Arithmetic operators

  • Standard arithmetic operations + - * /
  • Remainder % is a binary operator that returns the remainder of dividing it’s two operands
  • Increment ++ and decrement -- 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.

Bitwise operators

Yeah… I kinda understand these (on some level), but also that this is not where I need to dwell.

Logical operators

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 ! returns false if the operand can be converted to true

they are tested for short-circuit evaluation using the following rules:

  • false && anything is short-circuit evaluated to false
  • true || anything is short-circuit evaluated to true

String operators

  • + is the concatenation operator
  • += can also be used to concatenate strings, and is a shorthand assignment operator

To be continued…!