Bytes

JavaScript Statements

Module - 4 JavaScript Control Flow Operations
JavaScript Statements

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:

var myName = "John";

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:

myName = "Jane";

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:

var x = 5 + 3;
var y = 10 - 5;
var z = 2 * 6;
var w = 10 / 2;

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.

Online Javascript Compiler
Open Compiler
Recommended Courses
Certification in Full Stack Web Development
Course
20,000 people are doing this course
Become a job-ready Full Stack Web Developer in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.
Masters in Computer Science: Software Engineering
Course
20,000 people are doing this course
Join India's only Pay after placement Master's degree in Computer Science. Get an assured job of 5 LPA and above. Accredited by ECTS and globally recognised in EU, US, Canada and 60+ countries.

AlmaBetter’s curriculum is the best curriculum available online. AlmaBetter’s program is engaging, comprehensive, and student-centered. If you are honestly interested in Data Science, you cannot ask for a better platform than AlmaBetter.

avatar
Kamya Malhotra
Statistical Analyst
Fast forward your career in tech with AlmaBetter

Vikash SrivastavaCo-founder & CPTO AlmaBetter

Vikas CTO

Related Tutorials to watch

Top Articles toRead

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

© 2023 AlmaBetter