Before you write your first line of JavaScript code, there are a few basic guidelines and syntax conventions that you should be made aware of.
This lesson will discuss some of these guidelines so that you can begin writing real JavaScript code in the next lesson of this course.
The ;
Character in JavaScript
In JavaScript, a ;
character is used to terminate almost every line of code. A line of code that ends in ;
is referred to as a statement
in JavaScript.
Statements are instructions to a computer to perform some specified task. We will learn more about statements in the next lesson of this course.
It is important to note that while every JavaScript statement must end with ;
, not every JavaScript line must end with ;
.
As an example, there are special examples of code called code blocks
(which can be identified by the use of {
and }
characters) that do not require every line to end in ;
.
An example of a code block is below.
if (hour < 12) {
greeting = "Good morning";
}
Notice how even though there are three lines of code in this example, only one ends with the ;
character.
One last item with respect to the ;
character - you may read that it is possible to run JavaScript code without the ;
character. This is true and is due to something called ASI, which stands for automatic semicolon insertion
.
ASI is an advanced topic and will not be covered in this course, so make sure to include the ;
character at the end of each of your JavaScript statements!
How To Print To The Console in JavaScript
Printing data and variables to your web browser's console is a very useful tool for debugging and learning code. Every language has a different syntax for printing data.
In JavaScript, you can print data using the console.log
statement.
As an example, let's say that you wanted to print the string Print me out!
in JavaScript. Here's how you would do this:
console.log('Print me out!');
How To Write Comments in JavaScript
In computer programming, comments are code that is not run by the computer, but is still visible to humans that are reading the code.
Just like with print statements, every language has a slightly different method for how to write comments. JavaScript comments are written using two instances of the /
character.
An example of this is below:
//This is a comment and will not be executed by the computer
Final Thoughts
In this lesson, you learned the following topics:
- How to use the
;
character in JavaScript - How to use the
console.log
statement in JavaScript to print data - How to write comments in JavaScript
In the next lesson, you will expand on this knowledge by learning how to write variables and statements in JavaScript.