Most programming languages you'll encounter are called "imperative" programming languages. JavaScript is one such language. Imperative means command-like you give the computer a series of commands, one after another. First do this, then do that.
These individual commands are called statements. You can compare them to sentences in English. They bring meaning by themselves and don't need anything else. "The man eats bread" is a complete sentence. It brings meaning on its own. A sentence in English is always terminated by a full stop.
Similarly, a statement in JavaScript should provide a command by itself. JavaScript statements are (almost always) terminated by a semicolon.
This is a complete statement:
let name = "HackYourFuture";
It's a complete command: declare a variable name and initialise it with "HackYourFuture". JavaScript doesn't need any additional information. The statement ends with a semicolon.
However, this is not a complete statement:
4 + 5
This equals 9, but what should JavaScript do with it? It doesn't provide a command. You need to do something with it, for example, let x = 4 + 5; or console.log(4 + 5). We call these parts of statements expressions. Expressions are not terminated by semicolons. They always "evaluate to a value". In our example, the expression 4 + 5 evaluates to 9. If expressions cannot be evaluated to a value, they are invalid. For instance, 4 + is not a valid expression—it is incomplete because we need something after the plus sign.
Statements can contain expressions. Can expressions contain statements? No, they cannot. However, expressions can contain other expressions. Consider 4 + 5: it contains the expressions 4 and 5, as these both evaluate to a value. The expression 4 evaluates to the number 4 - a very simple expression. Similarly, true, null, and undefined are all expressions.
In JavaScript, spaces, tabs, and newlines are generally ignored by the JavaScript engine. This means you can format your code with spacing and line breaks to make it more readable for developers without affecting how the program runs.
The engine cares about the structure and syntax of your code, not the whitespace between elements (with a few exceptions, like inside strings).
With spaces and newlines
let firstName = "John";
let lastName = "Doe";
if (firstName === "John") {
console.log("Hello, " + firstName);
}
Spaces removed
let firstName="John";let lastName="Doe";if(firstName==="John"){console.log("Hello, "+firstName);}
Both versions of the code work exactly the same way and produce the same result. However, the version on the left is much easier to read and understand. We use spaces, indentation, and newlines to make our code more readable for ourselves and other developers. Good formatting is essential for maintaining and collaborating on code!
<aside> 💭
We use spaces, indentation, and newlines to make our code more readable for ourselves and other developers. Good formatting is essential for maintaining and collaborating on code!
</aside>
In JavaScript, a semicolon (;) is used to mark the end of a statement. It tells the JavaScript engine where one instruction ends and the next one begins.
Semicolons serve as separators between statements in your code. They make it clear to both the JavaScript engine and other developers where each statement finishes.
let firstName = "John";
let lastName = "Doe";
console.log(firstName + " " + lastName);
Each line ends with a semicolon, clearly marking the end of each statement.
JavaScript has a feature called Automatic Semicolon Insertion (ASI), which means the engine can often understand your code even if you forget to add semicolons. It will automatically insert them where it thinks they should be.
let firstName = "John"
let lastName = "Doe"
console.log(firstName + " " + lastName)
This code will work because JavaScript automatically inserts semicolons at the end of each line.
However, relying on ASI can lead to unexpected bugs and errors. There are cases where JavaScript might insert a semicolon in the wrong place or fail to insert one where needed, causing your code to behave incorrectly.
let result = 1 + 2
(3 + 4).toString()
// JavaScript interprets this as:
// let result = 1 + 2(3 + 4).toString()
// This will cause an error!
<aside> ❗
Always add semicolons at the end of your statements. This makes your code more predictable, easier to read, and helps prevent bugs that can be difficult to track down.
</aside>
Comments are pieces of text in your code that are ignored by the JavaScript engine. They are used to explain code, make notes, or temporarily disable code without deleting it.
There are two types of comments in JavaScript:
Inline comments start with // and continue to the end of the line. They are useful for brief explanations.
// This is an inline comment
let age = 25; // You can also place comments after code
Multiline comments start with /* and end with */. They can span multiple lines and are useful for longer explanations or temporarily disabling blocks of code.
/*
This is a multiline comment.
It can span several lines.
Useful for detailed explanations.
*/
let name = "Alice";
/*
You can also use it to disable code:
let oldCode = "This won't run";
*/
The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 **
