Javascript Conditional Statements
When we make a decision based on another condition then this is called a conditional statement. Like, If it’s raining today, you won’t go to school. Here, going to school or not depends on whether it will rain today or not. In javascript, there are three kinds of conditional statements.
- If — statement
- If…else — statement
- If…else if — statement
Sometimes we use another statement called the switch statement.
If statement
The If statement is usually used to specify a block of JavaScript code to be executed if a condition is true.
if (the condition satisfy) {
Then execute the other operations.
}
It only checks the initial condition. If it is true or satisfying then the others will execute.
Example:
aif ( 23 < 45) {
console.log("Execute complete.");
}
var myName = 'Jenny';
if (myName === 'Jenny'){
console.log("Hello Jenny.");
}
var myName = 'karim';
if (myName === 'KARIM'){
console.log("My name Karim.");
}
// Here, the condition doesn't satisfy that's why output shows undefined.
Output:
>>> Execute complete.
>>> Hello Jenny.
>>> Undefined.
Note: In this way, it can be written several times in a row –
var myAge = 19
if (myAge < 10){
console.log("You are a kid.");
}
if (myAge <= 18){
console.log("You are a adult.");
}
if (myAge > 18){
console.log("You are a young man.");
}
Output:
>>> You are a young man.
If…else Statement
Use the else statement to specify a block of code to be executed if the condition is false then.
Example:
var myAge = 19
if (myAge < 18){
console.log("You are a kid.");
}
else {
console.log("You are adult.");
}
Output:
>>> You are an adult.
If…else if Statement
Here you can apply conditions as many as you desire by applying else if conditions. Whenever any condition is met his subordinate operation will be completed.
Example:
var whatDay = 'sunday';
if (whatDay === 'friday'){
console.log("It is closed today.");
}
else if (whatDay === 'thursday') {
console.log("It is partially closed today.");
}
else if (whatDay === 'saturday') {
console.log("Some office are closed today.");
}
else{
console.log("It's normal working day today.");
}
Output:
>>> It's a normal working day today.
Nested If Statement
A nested if is an if statement that is the target of another if or else. Nested if statements mean an if statement inside another if statement
Example:
var number = 12;
if (number > 1){
if (number > 10){
console.log("Greater than 10.");
}
console.log("Somewhere between 2 - 10");
}
Output:
>>> Greater than 10.
Switch Statement
The switch statement evaluates an expression, matching the expression’s value to a case clause, and executes statements associated operation with that case, as well as statements in cases that follow the matching case.
Example:
var weekDay = 'sunday';
switch (weekDay){
case 'saturday':
console.log("Saturday !! Today is closed.");
break;
case 'sunday':
console.log("Sunday !! It is a normal day.");
break;
case 'monday':
console.log("Sunday !! It is a normal day.");
break;
case 'sunday':
console.log("Monday !! It is a normal day.");
break;
case 'tuesday':
console.log("Tuesday !! It is a normal day.");
break;
case 'wednesday':
console.log("Wednesday !! It is a normal day.");
break;
case 'friday':
console.log("Friday !! Today is closed.");
Break;
default:
console.log("Not a day.");
}
Output:
>>> Sunday!! It is a normal day.
Note: Here, we use a new keyword named “break” to stop the execution. Otherwise, it will execute from there to the very end where your value matches. If there is no match case it will execute the default case.
Conditional statements are an essential feature of programming, including JavaScript. JavaScript conditional statements allow developers to create logic that executes only if certain conditions are met, making it possible to create dynamic and responsive applications. Conditional statements in JavaScript are used to control the flow of code execution based on specific conditions.
Conditional statements can be used to check for the validity of user input, validate form data, make decisions based on user actions, and more. For example, a website that requires a user to enter a valid email address can use JavaScript conditional statements to check whether the input is in the correct format. If the input is not valid, the website can display an error message to the user, telling them what needs to be corrected.
JavaScript conditional statements include if/else statements, switch statements, and ternary operators. If/else statements are used to check a single condition and execute code if the condition is true or false. Switch statements are used to check multiple conditions and execute different blocks of code based on the result of the check. Ternary operators are used to assigning a value to a variable based on a condition.
JavaScript conditional statements are important for creating code that is dynamic, interactive, and responsive to user input. They make it possible to create applications that can adapt to changing conditions and provide a better user experience. By understanding conditional statements, JavaScript developers can create more complex and powerful applications that can perform a wide range of tasks.