We've already seen how we can use the
= operator to set a variable equal to a value.
> const favoriteNumber = 42; > favoriteNumber; 42
= is called an assignment operator because it assigns the value on the right of the operator to the variable on the left. In the above example, favoriteNumber is assigned the value 42.
Another assignment operator is the
+= operator, because it too assigns a new value to the variable on the left based on the value to the right.
> let myNumber = 5; > myNumber += 1; > myNumber; 6
There is an assignment operator for each of the mathematical functions: addition, subtraction, multiplication and division.
+= -= *= /=
When you use any of these assignment operators, the value of the variable on the left side is changed by the math operation and value on the right. Let's do one of each:
> let testNumber = 10; >testNumber += 5; >testNumber; 15 > testNumber -= 9; >testNumber; 6 >testNumber *= 3; >testNumber; 18 >testNumber /= 2 >testNumber; 9
Assignment operators change the value of the variable on the left of the operator. Comparison operators do not change any values, but return
false depending on whether the statement evaluates as true or false.
> const myNumber = 5; > myNumber < 10; true > myNumber > 10; false > myNumber === 10; false > myNumber === 5; true
Notice the triple equals operator. When we're asking whether something is equal, we use
=== (3 equal signs). When we're setting a variable equal to something, we use
= (single equal sign). Mixing these up is one of the easiest syntax errors to make.
You should have seen that the comparison operators return one of two values:
false. Notice that there are no quotes around these values.
false aren't strings - they're called booleans. They simply represent being true or false.
Comparison operators work for strings as well.
> const greeting = "hello world"; > greeting === "hello world"; true > greeting === "goodbye"; false
Notice that if you type
greeting after this, you will see that the variable greeting still contains the string "hello world". Comparison operators do not change the value of the variable. Let's look at another example to illustrate that important difference between assignment operators and comparison operators.
> const myNumber = 5; > myNumber === 5; true > myNumber === 10; false > myNumber = 10; > myNumber === 10; true > myNumber === 5; false
Common comparison operators:
5 === 5or
"cat" === "cat"evaluate to
3 === 5or
"cat" === "dog"evaluate to
!==means "not-equal-to". It is the opposite of
"cat" !== "dog"evaluates to
5 !== 5evaluates to
false, because saying that 5 is not equal to 5 is not true.
>means "greater-than (and not equal to)".
3 > 4evaluates to
3 > 3also evaluates to
false, because 3 is equal to 3 and not greater; and
3 > 2evaluates to
>=is the same as
>, except it evaluates to
trueif the two sides are equal.
3 >= 3evaluates to
true, and so does
3 >= 2.
<is the opposite of
>. It means "less-than (and not equal to)".
3 < 5evaluates to
3 < 3evaluates to
falsebecause they are equal.
<=is the opposite of
>=. It means "less-than-or-equal-to".
3 <= 3evaluates to
truebecause 3 is equal to 3.
3 <= 1evaluates to
3 <= 5evaluates to
false are booleans. They are not strings - they simply represent being true or false.
Assignment operator: Changes the value of the variable on the left of the operator.
Comparison operator: Does not change any values, but returns a boolean (
false) depending on whether the statement evaluates as true or false.
Be aware of the difference between the
= assignment operator and the
=== comparison operator.
=assign variable on left of operator value on right of operator
+=increase value of variable on left of operator by value on right of operator
-=decrease value of variable on left of operator by value on right of operator
*=multiply value of variable on left of operator by value on right of operator
/=divide value of variable on left of operator by value on right of operator
===is equal to
!==is not equal to
>=greater than or equal to
<=less than or equal to
Lesson 15 of 23
Last updated June 18, 2020