JavaScript Statements

JavaScript statements are the individual instructions that make up a JavaScript program. They are executed by the JavaScript interpreter one by one, in the order they appear in the code.

Examples of JavaScript statements include variable declarations, function definitions, loops, and conditional statements.

Here are a few examples of JavaScript statements:

Variable declaration:

let x = 5;

Function definition:

function add(a, b) {
  return a + b;
}

Conditional statement:

if (x > 0) {
  console.log("x is positive");
} else {
  console.log("x is non-positive");
}

Loop:

for (let i = 0; i < 10; i++) {
  console.log(i);
}

Expression statement:

x++;

Object creation:

let person = {firstName:"John", lastName:"Doe", age:50};

Calling a function:

let result = add(1, 2);

Calling a method:

let text = "Hello";
console.log(text.toUpperCase());

These are just a few examples of the many types of statements that can be used in JavaScript. There are many more that can be used to perform a wide range of operations.

Semicolons in JavaScript

In JavaScript, semicolons are used to separate statements. For example, without a semicolon ; the following code would be treated as one statement and would cause an error:

let x = 5
let y = 10

with semicolon it will be treated as two different statement

let x = 5;
let y = 10;

JavaScript’s interpreter uses a process called Automatic Semicolon Insertion ASI to help determine where statements end. It does this by looking for certain characters, such as line breaks, to determine where a statement ends. However, this can lead to unexpected behavior and errors if not used properly.

In general, it is recommended to use semicolons consistently to separate statements to make the code more readable and to prevent potential errors.

White spaces in JavaScript

In JavaScript, white spaces, also known as whitespace characters, refer to any non-printable characters, such as spaces, tabs, and line breaks. They are used to separate and format the code, making it more readable to humans.

JavaScript’s interpreter ignores white spaces, meaning they do not affect the execution of the code. This means that you can use as many or as few white spaces as you like, and the code will still run the same way.

For example, the following two lines of code are equivalent:

let x = 5;
let     x     =     5;

However, it is still good practice to use white spaces consistently to format the code, making it more readable and easier to understand.

For example, when writing a function, it’s considered a best practice to put a space between the keyword function and its name, between the name and the parameter list, and between the parameter list and the code block.

function add(a, b) {
  return a + b;
}

In addition, when indenting code blocks, such as the one in the above example, it’s considered best practice to use tabs or spaces to indicate the level of indentation.

Overall, while JavaScript ignores white spaces, it’s still important to use them consistently to format the code and make it more readable.

Line breaks in JavaScript

In JavaScript, line breaks, also known as newlines, refer to the characters that separate lines of code. They are used to format the code, making it more readable to humans.

JavaScript’s interpreter generally ignores line breaks, meaning they do not affect the execution of the code. This means that you can use as many or as few line breaks as you like, and the code will still run the same way.

However, line breaks can be significant in certain situations, such as in the Automatic Semicolon Insertion (ASI). ASI is a process used by the JavaScript interpreter to determine where statements end.

The interpreter looks for certain characters, such as line breaks, to determine where a statement ends. This can lead to unexpected behavior and errors if not used properly, and that’s why it’s recommended to use semicolons to separate statements to make the code more readable and to prevent potential errors.

Otherwise, it is still good practice to use line breaks consistently to format the code, making it more readable and easier to understand.

For example, when writing a function, it’s considered a best practice to put a line break after the function’s name, and after the code block, like this:

function add(a, b) {
  return a + b;
}

Overall, while JavaScript ignores line breaks, it’s still important to use them consistently to format the code and make it more readable.

JavaScript Code Blocks

In JavaScript, a code block refers to a section of code that is executed as a unit. A code block is defined by a pair of curly braces {}, and the code inside the curly braces is considered to be part of the code block.

Code blocks are used in a variety of contexts in JavaScript, including:

Conditional statements:

Code blocks are used to define the code that should be executed if a certain condition is true.

For example:

if (x > 0) {
  console.log("x is positive");
} else {
  console.log("x is non-positive");
}

Loops:

Code blocks are used to define the code that should be executed repeatedly while a certain condition is true.

For example:

for (let i = 0; i < 10; i++) {
  console.log(i);
}

Functions:

Code blocks are used to define the code that should be executed when a function is called.

For example:

function add(a, b) {
  return a + b;
}

Scoping:

Code blocks can also be used to define a new scope in JavaScript, which can be used to create and manage variables.

Code blocks are an important feature of JavaScript, allowing you to group statements together and control the flow of execution in your code.

It’s important to note that, JavaScript uses a syntax called hoisting where all variable and function declarations are moved to the top of their scope before the code is executed.

You can use let and const instead of var, to avoid hoisting and use block scoping.

Related Posts: