Statements in JavaScript
A statement in JavaScript is an instruction or command that carries out a certain action. Statements can be thought of as human language’s equivalent of whole sentences. A list or series of statements is the basic building block of a JavaScript application.
The essential building elements of JavaScript applications are statements. By default, these statements are executed sequentially by a JavaScript interpreter in the order they are written. Except in cases where special control statements modify the flow of control, this sequential execution takes place from top to bottom.
Most statements are separated by semicolons. Programmers often mark statement ends with semicolons, even when they are not required. But in JavaScript, if statements are written on different lines, you may typically skip the semicolon. There is a feature called “automatic semicolon insertion” that is responsible for this, however it is not always effective. Although several statements on one line are acceptable as long as they are separated by semicolons, it is usually preferable for readability to use line breaks to put each statement on a separate line.
To accomplish specific objectives, JavaScript defines a variety of statement types:
- Expression statements can be used as statements and have side effects like assigning a value to a variable or running a function.
- Declaration statements: These define new variables or functions using let, const, or var.
- Control structures: These declarations change the execution flow’s default top-to-bottom direction. Some examples are as follows:
- Conditional statements: If, else, and switch are conditional statements that, depending on how an expression is evaluated, either execute or skip statements. The value of an expression can be used to select from a number of code blocks using the switch statement. If a condition is true, an if statement runs code; if the condition is false, an optional else clause runs code.
- Looping statements: While a condition is met, looping statements (while, do…while, for, for…in, and for…of) repeat the execution of statements. A condition, a statement that is run after each iteration, and variable initialisation are the three components of the for loop, for example. While a condition is true, the while loop runs code. The do…while loop checks the condition after running its code block at least once.
- Jump statements: The interpreter jumps to a different section of the program when it encounters jump statements (break, continue, return, throw). Break ends a loop or switch, whereas continue moves on to the following loop iteration. In functions, return is used to give back a value.
- Compound statements: A group of statements encapsulated in curly braces ({}) are regarded as a single unit and are referred to as compound statements or block statements. When combined with control flow expressions like loops and conditionals, these blocks enable the execution of several statements even when the grammar strictly only expects one. Braces can be used to describe an object literal or to begin a statement block in JavaScript.
Statements, variable declarations, loops, and other constructions are grouped together to form functions. When you call a function, this full set of statements is executed. The statements in between the curly braces make up the body of a function.
All of the code referenced or contained within a document, including external files and inline scripts, makes up a JavaScript program that runs in a web browser. A single global context unites these disparate parts of code. Script execution, in which code is executed from top to bottom within each script, usually takes place first. This is followed by an event-handling phase that is brought on by user interactions or other events.
This straightforward code sample shows a statement block and multiple statements:
// This is a single-line comment
// Variables are declared with keywords
let score = 0; // Declare and initialize a variable. This is a statement
// Assigning a new value is also a statement
score = score + 10; // Use spaces around operators
// Function calls are often expression statements
console.log(“Score after first turn: ” + score); // Print the value. This is a statement
// An if statement is a control structure statement
if (score > 5) { // The condition is an expression. Curly braces start a statement block.
// The code inside the curly braces is a statement block
console.log(“Score is greater than 5.”); // Another statement inside the block
let bonus = 5; // Another statement – variable declaration within the block
score = score + bonus; // Another statement – assignment
} // End of the statement block. No semicolon after the closing brace of a block.
console.log(“Final score: ” + score); // Another statement
/*
This is a multi-line comment.
It can span several lines.
Multi-line comments can be used for documentation,
like outlining the purpose of a file.
*/
Variable declarations (let), assignment statements (=), function calls (console.log), and a conditional statement (if) that holds a block of other statements are all displayed in this example. A statement is made by each line that completes a certain activity and ends with an explicit or implicit semicolon. The program carries out these statements one after the other.