Loading course content...
Loading course content...
Code Playground is only enabled on larger screen sizes.
In this lesson, we are going to introduce some basic concepts in JavaScript.
An expression is a piece of code that gives a value. It is made of values and operators that evaluates to another value.
1 + 2;
true && false;
"John" + " " + "Doe";For the first example, 1 and 2 are numeric values, and the plus sign (+) is an operator. The entire expression evaluates to another numeric value, 3.
An operator is a special symbol that performs certain operations on values. For example, the addition operator (+) adds two values together.
There are several other operators in JavaScript, and we will talk more about them later.
A statement is an instruction that performs certain actions, returns a value, or both. A full statement should end with a semicolon (;).
myFunc(); // -> A function call that doesn't return a value
true && false;
"John" + " " + "Doe";A statement can contain one or more expressions. For example:
console.log("John" + " " + "Doe");In this case, console.log() is an expression, and "John" + " " + "Doe" is also an expression. Together, they form a statement.
A variable is a way for the program to "remember" a value so that it can be reused later.
To declare a variable, you need to use the keyword let, const, or var (outdated).
let x = 10;Followed by the variable name:
let x = 10;The assignment operator:
let x = 10;And finally, the value that you wish to assign to the variable.
let x = 10;In this example, the value 10 is assigned to the variable x.
The variable x can then be used later in the program:
let x = 10;
x + 10; // -> 20
x * x; // -> 100Variables can be declared in 3 different ways in JavaScript:
let x = 10;
const x = 10;
var x = 10;let declares a mutable variable, meaning the value can be changed later in the program.
let x = 10;
x + 10; // -> 20
x = 5; // assign a new value to x
x + 10; // -> 15const declares a immutable variable, meaning you cannot change its value after declaration. Attempting to do so will result in an error .
const x = 10;
x + 10; // -> 20
x = 5; // -> Error/Users/. . ./index.js:5
x = 5; // -> Error
^
TypeError: Assignment to constant variable.
at Object.<anonymous> (/Users/. . ./index.js:5:3)
at Module._compile (node:internal/modules/cjs/loader:1378:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
at Module.load (node:internal/modules/cjs/loader:1212:32)
at Module._load (node:internal/modules/cjs/loader:1028:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
at node:internal/main/run_main_module:28:49
Node.js v21.6.0var is the legacy method of declaring a variable. It is rarely used these days, but you might still encounter it in some old software. It works differently in terms of scope compared to let and const. We will explain the details later.
The keywords in JavaScript are a set of words with special meanings and purposes, such as the let, const, and var that are used to declare a new variable.
The keywords are case sensitive, meaning JavaScript will not treat LET or Let as a keyword.
You probably already know what comments look like from our previous examples, but there is still something we must discuss.
// Execute if <condition> returns trueThis is a single-line comment. It is the part of your code that is intended for humans to read and will be ignored by the JavaScript compiler.
You can also define a section of texts as comments using /* and */.
/*
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.
*/All the enclosed texts will be ignored by JavaScript.
1 + 2; true && false; "John" + " " + "Doe";