Congratulations - yesterday we wrote the Hello World! program together!
console.log("Hello World!");
The command console.log()
is how we write output to the developer console. You can put words, numbers, and more inside the brackets to have them show up on the screen. For words (characters) we need to use quotation marks: console.log(" ")
but for numbers we can just put the value: console.log(3.14)
We can also separate items with a comma (,) to print more than one thing! It will be separated by a space:
console.log("This","is","text.",15) // Outputs: This is text. 15
Most programming languages use some special commands for things like a new line or tab (indent). These are called escape sequences and we use a backslash \
to "escape" them.
Sequence | Usage |
---|---|
\n | Most common - used for a new line. |
\r | Used for a carriage return (not used often) |
\t | Used to insert a horizontal tab (indent) |
\v | Used to insert a vertical tab |
\" | Used to insert a single double-quote character |
\' | Used to insert a single single-quote character |
"\" | Used to insert a blackslash character |
Remember that you need to use quotes if you want to print text.
console.log("Line 1.\nThis is \"text\"");
/* Output:
Line 1.
This is "text"
*/
One last thing - every time you use the console.log( )
command, it automatically adds a new line (\n) at the end.
(Read carefully and work with a partner if you are struggling - ask Mr. Brash only after asking a classmate)
- Open VSCode.
- Go to FILE > Close Folder (if that option exists)
- Go to FILE > Open Folder
- Create a new folder called "Unit 1" and select that
- Add a new file to VSCode
- Name the new file "1.1 - Output.js"
Now try the following coding tasks and "run" your code to test the output:
1. Add the appropriate code(s) to the JavaScript file 1.1 - Output.js
that will print the address for Apple Inc.:
Apple Inc.
One Apple Park Way
Cupertino, CA
95014
+1 408-996-1010
Microsoft Corporation
One Microsoft Way
Redmond, WA
98052
+1 425-882-8080
5. Finally, output the following to the console using one console.log( )
command and horizontal tabs between the letters:
x o x
o x o
x o x
When you are sure that you've followed the instructions perfectly, ask Mr. Brash to check your work.
🕒 If you finish with class time left, you can complete the ISU or take a look at go.brash.ca/karel