Bytes

JavaScript Statements

Last Updated: 25th April, 2024

Introduction to JavaScript Statements

JavaScript Statements are the building blocks of any JavaScript program. They are individual instructions that tell the computer what to do. When the browser executes JavaScript code, it processes the statements one by one, starting from the top of the file and working its way down.

A statement in JavaScript can be anything from a simple variable declaration to a complex loop or conditional statement. Each statement in JavaScript is separated by a semicolon (;).

There are many different types of statements in JavaScript, each with its own purpose. Understanding the different types of statements is essential to writing effective JavaScript programs.

Expression Statements

An expression statement is the simplest type of statement in JavaScript. It is a line of code that performs an action or produces a value.

console.log("Hello, World!"); // produces the output "Hello, World!"

In this example, the expression statement is console.log("Hello, World!");. This statement calls the console.log() function and passes the string "Hello, World!" as an argument. The function then outputs the string to the console.

Variable Declarations

Variable declarations are used to create variables in JavaScript. They allow you to store values and data for use later in your program. Here's an example of a variable declaration statement:

Loading...

In this statement, we're declaring a variable called myName and assigning it the value "John".

Assignment Statements

Assignment statements are used to assign values to variables that have already been declared. Here's an example of an assignment statement:

Loading...

In this statement, we're assigning the value "Jane" to the myName variable that we declared earlier.

Arithmetic Statements

Arithmetic statements are used to perform mathematical operations in JavaScript. Here are some examples of arithmetic statements:

Loading...

In these statements, we're performing addition, subtraction, multiplication, and division operations, respectively.

Conditional Statements

Conditional statements are used to make decisions in your program. They allow your code to choose between different courses of action depending on the values of certain variables or conditions.

let x = 10;
if (x > 5) {
  console.log("x is greater than 5");
} else {
  console.log("x is less than or equal to 5");
}

In this example, the conditional statement checks if the value of x is greater than 5. If it is, the first block of code is executed (console.log("x is greater than 5")); otherwise, the second block of code is executed (console.log("x is less than or equal to 5")).

Looping Statements

Looping statements are used to repeat a block of code multiple times. There are several different types of looping statements in JavaScript, including for loops, while loops, and do-while loops.

javascriptCopy code
for (let i = 0; i < 5; i++) {
  console.log(i);
}

In this example, the for loop is used to output the values of i from 0 to 4. The loop runs 5 times, incrementing i by 1 each time.

Jump Statements

Jump statements are used to break out of a loop or skip to the next iteration of a loop. The two most common jump statements in JavaScript are break and continue.

The breakstatement is used to break out of a loop early. Here's an example:

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

In this example, the break statement is used to exit the for loop when i reaches 5. This means that only the values of i from 0 to 4 are output to the console.

The continue statement is used to skip over certain parts of a loop. Here's an example:

for (var i = 0; i < 10; i++) {
  if (i === 5) {
    continue;
  }
  console.log(i);
}

In this statement, we're using a for loop to print the numbers 0 through 9 to the console, except for the number 5, which we're skipping over using the continue statement.

Conclusion

JavaScript statements are the individual commands or instructions that are used to create a program in JavaScript. These statements tell the computer what to do and how to do it. There are many different types of statements available in JavaScript, including variable declarations, assignment statements, arithmetic statements, conditional statements, loop statements, and jump statements.

Variable declarations are used to create variables in JavaScript. Assignment statements are used to assign values to variables that have already been declared. Arithmetic statements are used to perform mathematical operations. Conditional statements are used to make decisions. Loop statements are used to repeat a block of code multiple times. Jump statements are used to break out of loops early or skip over certain parts of a loop.

By using these statements effectively, you can create powerful and efficient JavaScript programs that can perform a wide variety of tasks.

Module 4: JavaScript Control Flow OperationsJavaScript Statements

Top Tutorials

Related Articles

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter