JavaScript : Statements

In programming, a statement in JavaScript is a complete set of instructions that performs a specific task. JavaScript statements are the building blocks of JavaScript programs and scripts. These statements are executed one by one, in the order they are written, to create the desired behavior or functionality in a web application. Here are some common types of JavaScript statements:

1. Variable Declaration:


Variable declarations are used to create variables and optionally initialize their values.

Using var (old way, function-scoped):
 
  var x = 10;
  var name = "John";

  

Using let (block-scoped, introduced in ES6):

  let y = 20;
  let message = "Hello";

 

Using const (block-scoped constant, cannot be reassigned):

  const PI = 3.14;
  const MAX_SIZE = 100;

 

2. Assignment Statements:


Assignment statements are used to assign values to variables.


x = 5;
name = "Alice";

 

3. Arithmetic Expressions:


JavaScript supports various arithmetic operations:


let sum = 5 + 3; // Addition
let difference = 10 - 5; // Subtraction
let product = 3 * 4; // Multiplication
let quotient = 12 / 3; // Division
let remainder = 10 % 3; // Modulus (remainder of division)

 

4. Conditional Statements:


Conditional statements execute different code based on certain conditions.

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

  

else if Statement:


  if (grade >= 90) {
      console.log("A");
  } else if (grade >= 80) {
      console.log("B");
  } else {
      console.log("C or below");
  }

  

5. Loops:


Loops are used to repeat blocks of code.

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

  

while Loop:


  let i = 0;
  while (i < 5) {
      console.log(i);
      i++;
  }

  

do...while Loop:

  let j = 0;
  do {
      console.log(j);
      j++;
  } while (j < 5);
  

6. Function Declarations:


Functions are reusable blocks of code that can be called with different inputs.


function greet(name) {
    return "Hello, " + name + "!";
}

console.log(greet("Alice")); // Outputs: Hello, Alice!
 

7. Switch Statement:


The switch statement is used to perform different actions based on different conditions.


let day = 3;
switch (day) {
    case 1:
        console.log("Sunday");
        break;
    case 2:
        console.log("Monday");
        break;
    default:
        console.log("Unknown day");
}

 

 8. Comments:


Comments are not executed by the JavaScript engine but provide information about the code for developers.

Single-line Comment:
 
  // This is a single-line comment
  

Multi-line Comment:
  
  /*
     This is a
     multi-line
     comment
  */

  

 Important Points:

 

  •  JavaScript statements should end with a semicolon ;, although it's optional due to automatic semicolon insertion.
  •  Blocks of code are defined by curly braces {}.
  •  JavaScript is case-sensitive.
  •  Always consider variable scope (function scope for var and block scope for let and const).
  •  Use comments to document and explain your code for better readability and maintainability.

Understanding JavaScript statements allows you to write scripts to control the behavior of web pages. These statements provide the logic and functionality necessary to create dynamic and interactive web applications.