Javascript Statements - 16 Statements (Reference)

Tech:
Javascript
Since:
2 years ago
Views:
6

The Javascript statements are the building blocks of Javascript. Following is the list of most used Statements in Javascript. It includes declarations, loops, control flow, and function.

Statements:

var

The 'var' keyword in the statement is used to declare a variable.

var x = 5;
var x = 5.1;
var x = "hello";
var x = true;
var x = x + x;

let

The 'let' keyword was introduced in ES6. The 'let' is used to declare a variable with block scope and it cannot be redeclared.

var x = 10;
{
  let x = 2;
  // Function scope
}

const

The 'const' keyword was introduced in ES6. The const keyword is used to declare a constant variable with block scope. The value of a constant variable cannot be changed.

var x = 10;
{
  const x = 2;
  // Function scope
  // Static
}

if/else

The 'if/else' is a conditional statement. The 'if' is used to execute a block of code if a condition is true. The 'else' is used to execute a block of code if the condition is false.

if (1 < 10) {
  // If condition 1< 10 is true
} else {
  // If condition 1 < 10 is false
}

for

The 'for' is used to execute a block of code a specified number of times.

for (var i = 0; i < 5; i++) {
  // This will execute 5 times
}

for/in

The 'for/in' is used to iterate over the properties of an object.

var object = { one: 1, two: 2, three: 3 };

for (var property in object) {
  // This will execute 3 times
}

for/of

The 'for/of' is used to iterate over the values of an array.

var numbers = ["one", "two", "three"];

for (var num of numbers) {
  // This will execute 3 times
}

while

The 'while' is used to execute a block of code while a condition is true.

var i = 0;

while (i < 10) {
  i++;
  // This will execute 10 times
}

do/while

The 'do/while' is used to execute a block of code at least once before checking the condition.

var i = 0;

do {
  i++;
  // This will execute at least once
} while (i < 10);

switch

The 'switch' is used to execute a block of code depending on different cases. If none of the cases are true, the default case is executed. The 'break' statement is used to break out of the switch.

var x = 2;

switch (x) {
  case 1: {
    // This will execute if x is 1
    break;
  }
  case 2: {
    // This will execute if x is 2
    break;
  }
  default: {
    // This will execute if none of the cases are true
  }
}

function

The 'function' is used to group a block of code together. It is executed when the function is called with its function name.

function functionName() {
  // Function body
}

function functionName(param1, param2) {
  // It will accept two parameters
}

return

The 'return' is used to return a value from a function. It will terminate the function and the code after the return statement will not be executed.

function functionName() {
  return "Hello world";

  // This will return "Hello world"
}

break

The 'break' is used to break out of a loop. It is used in 'switch' and 'for' loops.

for (var i = 0; i < 10; i++) {
  if (i === 3) {
    break;
    // This will break the loop
  }
}

continue

The 'continue' is used to continue to the next iteration of a loop. It is used in 'switch' and 'for' loops.

for (var i = 0; i < 10; i++) {
  if (i === 3) {
    continue;
    // This will skip the iteration
  }
}

try/catch

The 'try/catch' is used to handle errors. If there is an error, the 'catch' block will be executed. An 'error' object is passed to the 'catch' block. The 'final' block is executed always after the 'catch' block.

try {
  xalert("Hello world");
  // This will throw an error because xalert is not defined
} catch (err) {
  // This will execute if an error is thrown
} finally {
  // Will execute always
}

debugger

The 'debugger' is used to pause the execution of a program.

function functionName() {
  debugger;
  // This will pause the execution of the function
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements