JavaScript has a fairly large[e93] set of operators. Most of them work the same way as in other C-like languages. There are a few differences to watch out for.
The + operator is used for both addition[e94] and concatenation. If either of the operands is a string, it concatenates. This can cause errors. For example, '$' + 3 + 4 produces '$34', not'$7'.
+ can be used as a prefix operator, converting its string operand to a number.
!! can be used as a prefix operator, converting its operand to a boolean.
The && operator is commonly called logical and. It can also be called guard. If the first operand is false, null, undefined, "" (the empty string), or the number 0 then it returns the first operand. Otherwise, it returns the second operand. This provides a convenient way to write a null-check:
var value = p && p.name; /* The name value will
only be retrieved from p if p has a value, avoiding an error. */
The || operator is commonly called logical or. It can also be called default. If the first operand is false, null, undefined, "" (the empty string), or the number 0, then it returns the second operand. Otherwise, it returns the first operand. This provides a convenient way to specify default values:
value = v || 10; /* Use the value of v, but if v
doesn't have a value, use 10 instead. */
JavaScript supplies[e95] a set of bitwise[e96] and shift operators, but does not have an Integer type to apply them to. What happens is the Number operand (a 64-bit floating-point number) is converted to a 32-bit integer before the operation, and then converted back to floating point after the operation.
In JavaScript, void is a prefix operator, not a type. It always returns undefined. This has very little value. I only mention[e97] it in case you accidently type void out of habit[e98] and are puzzled by the strange behavior.
The typeof operator returns a string based on the type of its operand.