As in any programming language, JavaScript has a set of basic syntax rules that we must follow.
These rules define how to organize instructions and how to structure code so that the interpreter (the browser or the runtime environment) can process it without errors.
Let’s review them 👇
Instructions and Sequential Execution
In JavaScript, the code is composed of instructions that execute sequentially.
This means that the instructions run one after another, from top to bottom, in the order they appear.
let name = "Juan";
console.log(name);
In this example, the first line defines a variable called name
with the value "Juan"
, and the second line displays the content of the variable in the console.
Semicolon to End Statements
In JavaScript, each instruction generally ends with a semicolon ;
.
let name = "Juan";
console.log(name);
However, the language allows you to omit it in many cases. To do this, it applies some rules called ASI (Automatic Semicolon Insertion).
Read more about ASI Rules
The ASI (Automatic Semicolon Insertion) rules in JavaScript determine when the language’s engine automatically inserts a semicolon into the code. Here are the main rules:
- End of line: JavaScript inserts a semicolon when it encounters a line break at the end of a statement.
- Closing braces (}): If a statement ends with a closing brace, it is considered that the statement has finished, and no additional semicolon is required.
- Control keywords: Keywords like return, break, continue, and throw at the start of a line trigger the insertion of a semicolon if necessary.
- Operators followed by a new line: If a statement ends with an operator and continues on the next line, JavaScript inserts a semicolon where appropriate.
- Control structures: In structures like if, for, and while, closing braces indicate the end of the statement without the need for a semicolon.
Although JavaScript can generally infer the end of a line, it is good practice to always use a semicolon. Otherwise, it can lead to errors. We see this with a simple example.
Here JavaScript does not understand the end of the statement ending with 1+2
const c = 1 + 2
(1 + 2).toString()
It will give you an error like
TypeError: 2 is not a function
Because what JavaScript interpreted was this
const c = 1 + 2(1 + 2).toString()
Which is wrong, and that’s why it throws an error 😒
However, if we delimit the statements with a ;
const c = 1 + 2;
(1 + 2).toString();
JavaScript interprets it correctly and shows
'3'
In summary, always use semicolons.
Braces to Delimit Code Blocks
Braces {}
in JavaScript are used to define code blocks, especially in control structures like functions, loops, or conditions.
All the code within the braces belongs to the same block and will execute together (for the interpreter, it’s as if it were a single instruction).
Example of a code block in an if
structure:
if (name === "Juan") {
console.log("Hello, Juan!");
}
In this case, the code inside the braces will only execute if the condition (name === "Juan"
) is true.
Case Sensitivity
JavaScript is a language that is case-sensitive. This means it distinguishes between name
, Name
, and NAME
, considering them different variables.
let name = "Juan";
let Name = "Pedro";
console.log(name); // Prints "Juan"
console.log(Name); // Prints "Pedro"
Variable Names and Naming Rules
When naming variables in JavaScript, there are certain rules we must adhere to:
Cannot start with a number. A variable cannot begin with a digit.
let 1name = "Juan"; // Incorrect let name1 = "Juan"; // Correct
Cannot contain spaces. Variable names must be a single word without spaces.
let my name = "Juan"; // Incorrect let myName = "Juan"; // Correct
Cannot include special symbols like
!
,@
,#
,%
, among others, except for the underscore (_
) and the dollar sign ($
).let name$ = "Juan"; // Correct let _name = "Juan"; // Correct
Styling conventions. Although JavaScript does not require it, it is common to use camelCase for variable names, where the first word is in lowercase and additional words start with uppercase:
let myExampleVariable = "Example";
Reserved Words
JavaScript has a list of reserved words that cannot be used as variable names, function names, or identifiers.
Some reserved words include:
- Control flow:
if
,else
,switch
,case
,default
- Loops:
for
,while
,do
,break
,continue
- Variable and function declarations:
var
,let
,const
,function
,return
- Object and class manipulation:
class
,extends
,constructor
,super
- Special values:
null
,undefined
,true
,false
- Asynchronous operations:
async
,await
- Import/export operators:
import
,export
- Logical and arithmetic operators:
new
,delete
,typeof
,instanceof
These words are used by the language for specific functions, and using them as names will cause errors.