Blog

Uncommon Knowledge about JavaScript Operators
Posted on May 26, 2019 in JavaScript by Matt Jennings

Note: The majority of the information below was taken from Eloquent JavaScript, 3rd edition.

Special Numbers

There are three special values in JavaScript that are considered numbers but don’t behave like normal numbers.

Unary operators

Operators that use two values are called binary operators, while those that take one are called unary operators. The minus operator can be used both as a binary operator and as a unary operator.

Logical Operators

JavaScript supports three logical operators: andor, and not. The && operator represents logical and. It is a binary operator, and its result is true only if both the values given to it are true.

The || operator denotes logical or. It produces true if either of the values given to it is true.

Not is written as an exclamation mark (!). It is a unary operator that flips the value given to it—!true produces false, and !false gives true.

The last logical operator I will discuss is not unary, not binary, but ternary, operating on three values. This one is called the conditional operator (or sometimes just the ternary operator since it is the only such operator in the language). When it is true, it chooses the middle value, and when it is false, it chooses the value on the right.

undefined vs null

In JavaScript undefined means that a JavaScript variable has been declared but not yet assigned a value.

null is an assignment value. It can be assigned to a variable as a representation of no value.

Testing some things about null and undefined:

Automatic type conversion

When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it needs, using a set of rules that often aren’t what you want or expect. This is called type coercion.

The null in the first expression becomes 0, and the "5" in the second expression becomes 5(from string to number). Yet in the third expression, + tries string concatenation before numeric addition, so the 1 is converted to "1" (from number to string).

When something that doesn’t map to a number in an obvious way (such as "five" or undefined) is converted to a number, you get the value NaN. Further arithmetic operations on NaN keep producing NaN, so if you find yourself getting one of those in an unexpected place, look for accidental type conversions.

Short-circuiting of logical operators

The logical operators && and || handle values of different types in a peculiar way. They will convert the value on their left side to Boolean type in order to decide what to do, but depending on the operator and the result of that conversion, they will return either the original left-hand value or the right-hand value.

The || operator, for example, will return the value to its left when that can be converted to true and will return the value on its right otherwise. This has the expected effect when the values are Boolean and does something analogous for values of other types.

 

Leave a Reply

To Top ↑