To proceed with your JavaScript lessons on a web browser, you will first need an updated web browser. Edge, Chrome or Firefox will do just fine. These screenshots were made in Chrome.
On a Windows/Linux machine, press Ctrl-Shift-J.
On a MacOS machine, press Cmd-Shift-J.
Below is what you should see.
The encircled button is the tab you should be at. If you find yourself in any position where the display is unfamiliar to you, click on that button to bring you back to the console.
Test your console. Type x into the console.
You may see that the browser automatically tries to guess what you are typing. Normally it would be a great help, but in this case, it is an obstacle. Click anywhere else to get rid of it.
Press the Enter key. This means that you have effectively entered x into your browser console. What do you see?
You are seeing this error message because x has not yet been defined.
Click on the indicated button. This will clear the console.
We are going to learn how to assign variables. Think of variables as placeholders for values that can be assigned right away, or later.
Enter x1. You should see this error message. This is because x1 has not yet been declared.
Enter x1 = 5. Here, you have declared the variable x1 and assigned the numeric value of 5 to it.
Enter x2 = 8. Here, you have declared the variable x2 and assigned the numeric value of 8 to it.
Now if you enter x1, the value will be returned to you because there is an assigned value.
Same for x2.
Now enter X1. You should see an error message. This is because JavaScript is case-sensitive. x1 and X1 are therefore not the same thing. We have not yet declared X1.
Now enter X1 = 100, and enter X1. You should now see a value.
It's time to do some math! JavaScript arithmetic operators are as follows:
For those unfamiliar with the term "modulo", it basically means "remainder".
Enter these formulas. Get creative by using your own values. You should see results returned.
Note that any value divided by zero (0) will give you some kind of error.
Clear the console. Then enter x = 5 and y = 10. Then enter x + y. You should get 15.
Try using other arithmetic operators on x and y.
If you have done any kind of arithmetic before, this shouldn't be a problem. Sometimes in code, our numerical formulas are not as straightforward as x + y. This is where parentheses come in.
Declare variables x, y and z. Assign whatever values you wish, or use the ones provided as an example.
Enter (x - y) * z and x - (y * z). You should see the correct results displayed. Operations within parentheses will always be executed first.
This exercise is a classic beginners' programming puzzle, and is designed to help cement your understanding of variable value assignment and arithmetic operation.
Given that x is 10 and y is 20, swop their values (x is 20 and y is 10) without resorting to direct variable assignment or a temporary holding variable.
Direct assignment
x = 20
y = 10
Temporary holding variable
z = x
x = y
y = z
The answer is as follows:
First enter x = 10 and y = 20.
Enter x = x + y. Now the value of x is (10 + 20 = 30).
Enter y = x - y. Now the value of y is (30 - 20 = 10).
Enter x = x - y. Now the value of x is (30 - 10 = 20).
Finally, x is now 20 and y is 10.
Review Question: Can this answer be replicated using multiplication and division? What are the considerations?
Take the TTL Lesson 1 Quiz