2: Operators and Expressions
- Define a "code expression".
- Understand the operator's function within a code expression
- Use the Chrome DevTools Console to begin writing code expressions
This module is the beginning of our programming journey!
Programming is the act of writing code (instructions) for the computer to execute sequentially. It is the same as written instructions for a cooking recipe!
We will first start with learning how to write single lines of code before stringing them together into a bigger whole. Before long, you will be creating multi-line programs of your own!
An expression is a "valid unit of code that resolves to a value".
This does mean that there are invalid code that are "unresolvable" and will give errors - something that will be discussed at a later time.
There are two kinds of code expressions - (1) those that purely evaluate; (2) those that have a "side effect".
We are familiar with the expressions that purely evaluate. These are the arithmetic expressions we learnt in school:
5 + 6. This, we understand, evaluates itself to
An example of the second type expression type is this:
a = 3. This expression enters into a new, yet understandable realm. This expression uses the
=operator to assign the value
3to the variable
x. This expression will evaluate to
We will look into these expressions in greater detail within the sub-modules. At this point we want to first have a space to experiment with code...
The reason we ask every student to use Google Chrome for this course is to both standardise the tools everyone uses and, in our current context, this functionality we are exploring to begin our foray into programming.
- 1.Open a new tab in the Chrome browser by clicking File > New Tab; or pressing
Cmd+Tfor Mac, and
Cmd+Option+Ifor Mac or
Ctrl+Shift+Ifor Windows; or
F12; or right-clicking anywhere in Chrome and clicking Inspect.
- 3.Select the
Consoletab in Chrome Dev Tools.
Accessing the Developer tools in the Chrome browser setting
Console tab in Chrome DevTools
Chrome console allows for in-browser calculations
2 + 2;
4 * 2;
4 / 2;
4 - 2;
The input here is a mathematical equation typed in by the user, you. You have instructed the computer to perform a mathematical operation. The computer _returned _ an output, the evaluation of the equation.
These exercises may seem trivial, but mathematical operations are at the core of all computing instructions. Computers fundamentally compute. That being said, they can get things predictably wrong. Try:
0.1 + 0.2in your console. Did you get what you expect?
In the above code,
/are known as operators. Specifically, they are arithmetical operators: performing a mathematical operations between 2 numbers.
We will address all the arithmetic operators in the next section, and other operators in the following sections!.
Do not worry if you are afraid of math. This course only involves basic math. You are here to learn how to write code and not solve math problems! Hang in there 💪