A statement is a line of code or action that consist of values, variables, and operators. In the last lesson you created several statements that assigned values to variables using the assignment = operator. Statements can set values but do not become values themselves. Statements end with a semi-colon. In contrast, an expression is a piece of code that resolves to a value, i.e. values, calculations resulting in other values, variables, and groups of variables.
In addition, the assignment operator = is used for value assignment.
== (loose equals)
=== (strict equals)
!= (loose not equals)
!== (strict not equals)
The loose operators provide a way to compare two values of different types. For example “13” == 13 would yield true even though one is a string and one is a number.
< (less than)
> (greater than)
<= (less than or loose equals)
>= (greater then or loose equals)
Compound operators are useful for writing shorthand statements such as a = a + 2, which can make use of the compound variable +=. In this example a = a + 2 would become a += 2. Compound operations are available in each of the four operators:
Increment / decrement operators work in a similar way to compound operators whereby they provide a shorthand method of writing statements to increment or decrement values. For example a = a + 1 can use an increment variable of a++. Similarly a decrement take on this would look like a–. There are two increment and decrement operators:
Try it out
Open your script.js you created in the previous lesson and update with the following lines of code. (Note you can either add the following code on the end of your variables, datatypes code, or replace. In this lesson I will only be including the operators code relevant to this lesson).
After you have upated your script.js file, examine the results produced in the browser console.
How did you do? Were the results as you expected? Continue plugging in other values, variables and operators in order to practice these concepts further.
Precedence and Associativity
Remember math class when you had to work out precedence of values in a calculation?
4 + 6 * 9<br /> (4 + 6) * 9
4 + 6 > 9
Operator precedence determines the way in which operators are parsed with respect to each other. Operators with higher precedence become the operands of operators with lower precedence. So how do we keep track of what takes precedence? Thankfully, there is a very handy reference sheet included below, which lists all operators and associated precedence ranked from highest to lowest.
The reference sheet also includes associativity, which lets us know the direction in which the operation is executed. For example, typical math operations will be calculated left to right as you would be used. Other operators may cause execution to happen right to left. This is the case with the assignment = operator. Take the following statement:
<br /> let e,f;</p> <p>e = f = 4 + 6</p> <p>console.log(a);<br /> console.log(b);<br />
Notice that you can define more than one variable at a time in the example above. In this case the operation on the right is calculated and then at the end is assigned to b. The value of b is then assigned to a. This can be confirmed by examining at the values of a and b.
In this lesson you have learned about expressions, statements, operators, and operator precedence. The complete code used throughout this lesson is included below for your reference.
Image Credits: Photo by Dean Pugh on Unsplash.