JavaScript Statements And It's Example

Card image cap

JavaScript Statements Definition:

Statements define what the script will do and how it will be done. The end of a statement is indicated with a semicolon(;). The following are the types of statements in JavaScript:

  • Conditional Statement:
  • Loop Statements.
  • Object Manipulation Statements
  • Comment Statements
  • Exception Handling Statement

i). Conditional Statements

Conditional statemets are used to control the scripts so that different actions can be taken depending on the situation.

Conditional statements are used to decide the flow of execution based on different conditions. If a condition is true, you can perform one action and if the condition is false, you can perform another action.

The conditional statements included in the JavaScript code assist with decision making, based on certain conditions. The condition specified in the conditional statement can either be true or false.

The conditional statements execute the associated piece of code only if the condition is true. We have four types of conditional statements in JavaScript:

Type of Conditional Statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use if-else to specify a block of code to be executed, if the same condition is false
  • Use if-else-if ladder to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

(a). Simple if Statement

The if statements is the dundamental control statement that allows javaScript to make the descisions and execute statements conditionally.

Simple if Statement specify a block of code to be executed, if a specified condition is true

Syntax:

			
	if(expression)
	{
		statements(s) to be executed if expressionis true:
	}

Flow chart:

Example :

Result :

	Display "Good day!" if the hour is less than 18:00:

	Good day!


(b). If else Statement

The if...else statement is a form of control statement that allows JavaScript to execute statements in more controlled way.

If the condition evaluates to true then one block of statements will mget executed and if the condition is false other block of the statements will get executed.

It is also used to specify a block of code to be executed, if the same condition is false.

Syntax:

Flow Chart:


Example:

Result:


Click button to display a time-as per greeting:


(c). if-else-if ladder

The if if-else ladder statement is an advanced form of control statement that allows JavaScript to make correct decision out of several conditions.

A normal If Statement must be placed before the use the else If Statement. this is because the else If statement is an add-on to the simple if Statement.

if-else-if ladder can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax:


Flow chart:

Example:

Result :

a is the first letter

(d). Switch Statement

The basic syntax of the switch statement is to give an expression to evaluate and several different statements to execute based on the value of the expression.

The interpreter checks each case against the value of the expression until a match is found, If nothing matches, a default condition will be used.

The switch case statement has an expression which is compared with values of each case statement and if a match is found, the associated code is executed.

Syntax:

Example :

Result:

School: aimtocode.com
Std Name: Prayag
Result: Distinction
Card image cap