- Click the Menu button on the top right of your Google Chrome, choose More Tools, and the click on Developer Tools. you will see the same popup window, just click on Console tab.
- Right click on your Google Chrome, choose Inspect on the popup menu, and then click on Console.
Exercise 1.2. What is a computer program?
So can we print number? Sure. Type the following code:
We have numbers. Can we do some arithmetic? Of course:
console.log(135 + 246);
You will get 381, which is the sum of 135 and 246.
Here are some arithmetic operators:
+ : Addition
– : Subtraction
* : Multiplication
/ : Division
Now you understand console.log(), let’s do more exercises.
Exercise 1.3. Exercises for console.log()
- Print out your name in the console.
- Print out the sum of 1 to 10.
- Type in the following code and explain how it works:
console.log("I'm a " + "programmer");
- Print whatever you want in the console. Tip: if you want to write multiline command, use Shift + Enter to change line.
Exercise 1.4. Checkpoint
In this tutorial, you have learned:
- To use console.log() command to print string(text).
- To use console.log() to print numbers and to do some calculations.
Exercise 1.5. Conclusion
If you have any question about console.log(), feel free to ask in the comment below.
Exercise 2.1. Revise and warmup
- “Practice makes perfect.”
- The result of 2016 multiplies 2020.
Exercise 2.2. Popup message with alert()
alert("I'm a popup alert. Click Okay to exit."); window.alert("I'm also a popup alert. We are the same.");
alert(135 + 246); alert(2016 * 2020);
There you go, we got the answers. So we can print string(text) and number, can we print them together? Try the following code:
alert("The sum of 135 and 246 is " + 381); alert("The sum of 135 and 246 is " + 135 + 246); alert("The sum of 135 and 246 is " + (135 + 246));
The result of the first command is exactly what we want. But for the second, you got this:
If we want to do the addition for the numbers first, we need parentheses to change the execution order as in math. That’s how the third command works.
Exercise 2.3. alert()
Use alert() or window.alert() to send following messages:
- “Milk costs $2.”
- “Dinner costs $7.”
- “Today I spent: $” followed by the sum of 2 and 7.
Exercise 2.4. Checkpoint
In this exercise, you have learned:
- To use alert() and window.alert() to send popup messages.
- Print strings and numbers in one command.
- To use parentheses to change execution order.