Expressions
- Expressions are bits of code that express a computation that produces a value.
- We say an expression evaluates to a value or simply the value of an expression is some particular value.
- Anywhere a value of a given type is needed we can use any expression that produces values of that type.
Simple expressions
- Simple expressions are either literal values or variables.
- Literal values can be used as expressions. Their value is themself.
- Variables can be used as expressions. Their value is whatever value was last assigned to the variable.
Operators
- Operators define ways that values can be operated on to produce new values. For example in
1 + 2
, the +
sign is an operator which operates on the values 1
and 2
to produce the value 3
.
- Arithmetic operators operate on two numbers to produce a number. The six arithmetic operators we will use are
+
, -
, *
, /
, %
, and **
. You can also use a single -
to negate a numeric value. For example -x
is the negation of whatever the value of x
is.
- Logical operators operate on booleans to produce boolean values. The three logical operators are
&&
, ||
, and !
. &&
and ||
operate on two values while !
is written before a single expression and flips the logical value.
- Two operators operate on strings to produce new strings:
+
can be used to add together string by concatenation or smooshing them together: "abc" + "def"
evaluates to "abcdef"
. And the []
operator can be used to extract a single character from a string: "abc"[1]
evaluates to "a"
.
- Comparison operators operate on two numbers or two strings to produce boolean values. There are four of them:
<
, >
, <=
, >=
. For numbers they perform arithmetic comparison. For strings they compare strings alphabetically, i.e. one string is less than another if it would come before it in the dictionary.
- Equality operators operate on any two values to produce a boolean value. There are two that we will use:
===
and !==
. (These are called the “strict” equality operators. There are two other operators ==
and !=
, the “loose” equality operators that can consider values of entirely different types to be the same, e.g. true == 1
, and "123" == 123
. The loose equality operators are generally considered a mistake in Javascript’s design.)
- Operators have precedence similar to in math.
a + b * c
means evaluate a
and then evaluate b * c
and then add the two resulting numbers together. (Remember PEMDAS.)
- We can use paretheses (
()
) to group expressions if we want to change the order the operators are applied: (a + b) * c
means first evaluate a + b
and then evaluate c
and then multiply the two resulting numbers together.
Compound expressions
- Anywhere we can use an expression, we can use an arbitrarily complex compound expression made up of all of the kinds of expressions already discussed. For instance the
+
operator needs two values to operate on so it must be written between two expressions whose values. Those sub-expressions can be as simple as literal values: 1 + 2
or more complex: a * 2 + c * d / 10
Properties
- Some values have properties which are like variables but which are attached to another value. The first property we will use is the
length
property of string values. It is a number that tels us the number of characters in the string. Properties are accessed with the dot operator followed by the name of the property. So "abc".length
evaluates to 3. And s.length
evaluates to whatever the length of the current value of s
is.
Method calls
- Some values, notably strings, support methods which are used to compute new values from the value they are called on. For instance, if
s
is a string, s.slice(0, 3)
evaluates to a new string consisting of the first three characters of s
.
- Other methods on strings that we will use are
indexOf
, toUpperCase
, and toLowerCase
.
Types of expressions
- Most expressions can produce different values in different contexts (for instance depending on the value of variables used in the expression at the point the expression is evaluated) but we can usually describe the type of value—a number, a boolean, or a string—a given expression will produce. For instance
a && b
will always produce a boolean value (or an error if a
and b
do not themselves evaluate to boolean values).
- Javascript will often let you apply operators to values of the “wrong” type (such as using
&&
with numbers) by silently converting values from one type to another. But you should not rely on that and should only use operators with the type of values they are intended to be used with.