Day 4: how many types of an Conditional Statement ?
Conditional statements are used to perform different actions based on different conditions.
In JavaScript we have the following conditional statements:
● Use if to specify a block of code to be executed, if a specified condition is true
● Use else to specify a block of code to be executed, if the same condition is false
● Use else if 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.
If...else Statement:-
The if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax:-
● if (condition)
{
// block of code to be executed if the condition is true }
The else statement to specify a block of code to be executed if the condition is false.
Syntax:-
● if (condition)
{
// block of code to be executed if the condition is true }
else
{
// block of code to be executed if the condition is false }
The else if statement to specify a new condition if the first condition is false. Syntax:-
● if (condition1)
{
// block of code to be executed if condition1 is true
}
else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
}
else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Switch Statement:-
The switch statement is used to perform different actions based on different conditions.
Syntax:-
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
● The switch expression is evaluated once.
● The value of the expression is compared with the values of each case. ● If there is a match, the associated block of code is executed.
● If there is no match, the default code block is executed.
Switching Details:-
If multiple cases matches a case value, the first case is selected. If no matching cases are found, the program continues to the default label.
If no default label is found, the program continues to the statement(s) after the switch.
Loop Statement:-
Loops can execute a block of code a number of times.
● for - loops through a block of code a number of times
● for/in - loops through the properties of an object
● for/of - loops through the values of an iterable object
● while - loops through a block of code while a specified condition is true ● do/while - also loops through a block of code while a specified condition is true
For Loop :-
The for statement creates a loop with 3 optional expressions. Syntax:-
for (expression 1; expression 2; expression 3) {
// code block to be executed
}
Expression 1 is executed (one time) before the execution of the code block. Expression 2 defines the condition for executing the code block. Expression 3 is executed (every time) after the code block has been executed. Example:-
for (let i = 0; i < 5; i++) {
text += "The number is " + i + "\t";
} // 1 2 3 4 5
For in Loop :-
The JavaScript for in statement loops through the properties of an Object. Syntax:-
for (key in object) {
// code block to be executed
}
Example:-
const person = {fname:"John", lname:"Doe", age:25};
let text = "";
for (let x in person) {
text += person[x];
} // John Doe 25
● The for in loop iterates over a person object
● Each iteration returns a key (x)
● The key is used to access the value of the key
● The value of the key is person[x]
const number = [45, 4, 9, 16, 25];
let text = "";
for (let x in number) {
text += number[x];
} // 45 4 9 16 25
For of Loop :-
The JavaScript for of statement loops through the values of an iterable object. It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more.
Syntax:-
for (variable of iterable) {
// code block to be executed
}
Variable - For every iteration the value of the next property is assigned to the variable. Variables can be declared with const, let, or var.
Iterable - An object that has iterable properties.
Example:-
const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
text += x + “\t”;
} // BMW Volvo Mini
let language = "JavaScript";
let text = "";
for (let x of language ) {
text += x + “ ”; } // J a v a S c r i p t
While Loop :-
The while loop loops through a block of code as long as a specified condition is true.
Syntax:-
while (condition) {
// code block to be executed
}
Example:-
while (i < 10) {
text += i + "\t";
i++;
} // 0 1 2 3 4 5 6 7 8 9
Do while Loop :-
The do while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true
Syntax:-
do {
// code block to be executed
}
while (condition);
Example:-
do {
text += i + "\t";
i++;
} while (i < 10); // 0 1 2 3 4 5 6 7 8 9
Break Statement :-
The break statement "jumps out" of a loop.
Example:-
for (let i = 0; i < 10; i++) {
if (i === 3) { break; }
text += i + "\t";
} // 0 1 2
Continue Statement :-
The continue statement "jumps over" one iteration in the loop. The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
Example:-
for (let i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += i + "\t";
} // 0 1 2 4 5 6 7 8 9
Map :-
A Map holds key-value pairs where the keys can be any datatype. A Map remembers the original insertion order of the keys.
Method | Description |
new Map() | Creates a new map |
set() | Sets the value for a key in a Map |
get() | Gets the value for a key in a Map |
delete() | Remove a Map Element specified by the key |
has() | Returns a true if a key exists in a map |
forEach() | Calls a function for each key/value pair in a Map |
entries() Returns an iterator with the [key, value] pairs in a Map size Returns a number of elements in a Map
Example:-
// Create a Map
const fruits = new Map([["apples", 500],["bananas", 300], ["oranges", 200]]);
// Set Map Values
const fruits = new Map();
fruits.set("apples", 500);
fruits.set("bananas", 300);
// Get Map Values
fruits.get("apples"); // Returns 500
The size property returns the number of elements in a Map.
fruits.size; // 3
Set :-
A JavaScript Set is a collection of unique values.
Each value can only occur once in a Set.
Method | Description |
new Set() | Creates a new Set |
add() | Adds a new element to the Set |
delete() | Remove an element from a Set |
has() | Returns true if a value exists in the Set |
forEach() | Invokes a callback for each element in the Set |
entries() | Returns an iterator with all the values in a Set |
size Returns a number of elements in a Set
Example:-
const letters = new Set(["a","b","c"]); // Create a Set // Add Values to the Set
letters.add("a");
letters.add("b");
String
A JavaScript string is zero or more characters written inside quotes.
You can use single or double quotes.
Example:-
let a = “Hello World”; // Double quotes
let a = ‘Hello World’; // Single quotes
You can use quotes inside a string, as long as they don't match the quotes surrounding the string
Example:-
let answer = “It’s Beautiful…”;
let answer = “He is called ‘Johnny’”;
let answer = ‘He is called “Johnny”’;
Subscribe to my newsletter
Read articles from Jemin Kikani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by