javascript-sintaxis-basica

Basic Syntax of JavaScript

  • 5 min

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 the 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, code is composed of instructions that are executed sequentially.

This means that instructions are executed one after another, from top to bottom, in the order they appear.

let name = "Luis";
console.log(name);
Copied!

In this example, the first line defines a variable called nombre with the value "Luis", and the second line displays the variable’s content in the console.

Semicolon to End Statements

In JavaScript, each instruction typically ends with a semicolon ;.

let name = "Luis";
console.log(name);
Copied!

However, the language allows omitting it in many cases. For this, it applies rules called ASI (Automatic Semicolon Insertion).

Although JavaScript can generally infer the end of a line, it is good practice to always use the semicolon. Otherwise, it can cause errors. Let’s see a simple example.

Here JavaScript doesn’t understand that the instruction ends at 1+2

const c = 1 + 2
(1 + 2).toString()
Copied!

It will give you an error like

TypeError: 2 is not a function
Copied!

Because what JavaScript interpreted was this

const c = 1 + 2(1 + 2).toString()
Copied!

Which is wrong, and that’s why it throws an error 😒

However, if we delimit the instructions with a ;

const c = 1 + 2;
(1 + 2).toString();
Copied!

JavaScript interprets it correctly and shows

'3'
Copied!

In summary, always use a semicolon.

Braces to Delimit Code Blocks

Braces {} in JavaScript are used to define code blocks, especially in control structures like functions, loops, or conditions.

All code inside the braces belongs to the same block and will be executed together (for the interpreter, it’s as if it were a single instruction).

if (name === "Luis") {
    console.log("Hello, Luis!");
}
Copied!

In this case, the code inside the braces will only execute if the condition (nombre === "Luis") is true.

Case Sensitivity

JavaScript is a language that distinguishes between uppercase and lowercase letters (case-sensitive). This means it distinguishes between nombre, Nombre, and NOMBRE, considering them different variables.

let name = "Luis";
let Name = "Pedro";

console.log(name); // Prints "Luis"
console.log(Name); // Prints "Pedro"
Copied!

Variable and Function Names

When naming variables in JavaScript, there are certain rules we must respect:

  1. Cannot start with a number. A variable cannot start with a digit.
let 1name = "Luis"; // Incorrect
let name1 = "Luis"; // Correct
Copied!
  1. Cannot contain spaces. Variable names must be a single word without spaces.
let my name = "Luis"; // Incorrect
let myName = "Luis"; // Correct
Copied!
  1. Cannot include special symbols like !, @, #, %, among others, except for the underscore (_) and the dollar sign ($).
let name$ = "Luis"; // Correct
let _name = "Luis"; // Correct
Copied!
  1. Style conventions. Although JavaScript doesn’t require it, it’s common to use camelCase for variable names, where the first word is in lowercase and additional words start with capital letters:
let myExampleVariable = "Example";
Copied!

Reserved Words

JavaScript has a list of reserved words that cannot be used as names for variables, functions, or identifiers.

Some reserved words include:

  • Flow control: if, else, switch, case, default
  • Loops: for, while, do, break, continue
  • Variable and function declaration: 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 their use as names will cause errors.