Print a message
Everybody say "Hello!"
Let's look at some most basic JS statements. In the code editor window to the right of this tutorial page, you should have the following code:
// Lesson 1, console.log() & comments
console.log('Hello, Buzz Coder!');
console.log();
console.log("Hello, CodeCraft!");
Click the run
button at the top. In the console window below the code editor, you will see this text:
Hello, Buzz Coders!
Hello, CodeCraft!
Console.log()
console.log()
is a JS built-in function. It shows the data in the console window. When you type console.log()
, you are calling a function.
Here, the contents in the parentheses are called parameters or arguments. In the second console.log()
function, the empty parentheses means there are no parameters, so it printed out a blank line.
The parentheses are always required when calling a function in JavaScript, even if there are no parameters.
Now let's write some code. At the end of the file, start a new line and add a console.log()
statement to print out your own name: Hi, my name is ____.
Make sure that you begin your
console.log()
statement at the very beginning of the line, without spaces.In JavaScript, it's commonly considered a good habit to terminate each statement with a semicolon ; sign
Run the program again to see your message. It should look like this:
Hi, my name is Buzz.
Comments
The first line in the code editor starts with double slash //:
// Lesson 1, console.log() function & comments
This is a comment line.
Comments are text among your code but ignored when your program runs.
Their uses include allowing you to add annotations in your program to clarify the purpose of your code, or to disable a piece of code that you don't want to execute for now.
One-line comments start with //
. Everything to the right of the double slash becomes "commented out".
Now, add //
to the beginning of line 2, like this:
// Lesson 1, console.log() function & comments
//console.log('Hello, Buzz Coder!');
console.log();
console.log("Hello, CodeCraft!");
Click 'run' and you'll see the console result. The message Hello, Buzz Coder!
disappears because its console.log()
statement has been commented out, and the computer no longer reads it.
Notes:
The very first line of every CodeCraft file becomes the name of the file by default. You can see it shown at the file name box at the top right corner of your screen. Whenever you start a new file, it's a good idea to give it a name as a comment to indicate its content.