JavaScript conditions are used to control the behavior of any website or function. It is very important to learn JavaScript concepts to become a JavaScript programmer.
If, else, if else javascript conditions with example
JavaScript conditions are very important to learn for JavaScript developers. It is the basics of javascript programming language.
We use JavaScript conditional statements to create any conditions in our project. JavaScript conditions are used in a project when the client or user does not know the actual result or function.
Every javascript condition must have two values first one in value is true and the second is false. There are only two possible values of the javascript condition.
For example, if the user does not know any actual value then you will make every statement there that if the value is this, then the user or client will get the final result of that function and if it is not, then another function will be shown to the user as a result.
We will understand these conditions in our real life in such a way that when the light is coming in our house, then our computer system is turned on and if the light is not coming on, then the computer is turned off. In this example, light is our main condition and the computer system turns on and off according to the condition. It is a very simple and easy example to understand JavaScript conditions.
JavaScript conditions are divided into many types. Below are the top three types of JavaScript conditions. These three javascript conditions are the most useful condition in javascript programming. JavaScrropt condition is also known as a javascript conditional statement.
- If statement
- If else statement
- If else if statement
1. JavaScript If Statment
JavaScript if statement is the first and the most important javascript onions.JavaScrcrppt if statement is used to match or compare the value of one variable to another variable. Every javascript conditional statement has its own syntax or format to write in code. Below is a syntax of the if statement in JavaScript.
JavaScript If Statemnt Syntax
if( condition ){
Your Statment
}
Now in the below code example if the value of the variable firstName is equal to the value of the variable lastName then we will get a statement shown, otherwise, nothing will be shown.
var firstName = "Coding";
var lastName = "Coding";
if( firstName == lastName){
console.log("The full name is CodingPea")
}
2. JavaScript If Else Statment
If else statement is the second main condition in javascript. In the if-else statement, even if our condition is false, then some results should be shown to the user or the client. If the condition is true then your if condition statement should be shown but if your condition is false then your else statement should be shown as a result. Below is a syntax of the if else statement in javascript.
Syntax of if else statement
if( condition ){
Your Statment
}else {
your statment if condiotn is not true
}
In the below code example, if the value of variable firstName is equal to variable lastName then display "The full name is CodingPea" and if the value of both variables is not equal then display "The full name is not CodingPea".
var firstName = "Coding";
var lastName = "Pea";
if( firstName == lastName){
console.log("The full name is CodingPea")
}else {
console.log("The full name is not CodingPea")
}
3. JavaScript If Else If Statment
If else it is the third and most important javascript condition. It is a very easy but most useful condition in javascript. In the if else if statement we can use multiple conditions.
In the if else if condition, if your first condition is true, then the result will be shown, but if the first condition is false, then the second condition will be checked. In this case, if the second condition is still false then the other statement was shown to be false. Below is a syntax of the if else if statement in javascript.
Syntax of if else if statement
if( first condition ){
Your Statment of first condition
}else if( second condition ){
your statment of second condition
} else {
you statement if both above conditions are false
}
Below is a code example of an if else if statement:
var firstName = "Coding";
var lastName = "Pea";
if( firstName == lastName){
console.log("The full name is CodingPea");
}else if(firstName != lastName) {
console.log("The full name is not CodingPea");
} else {
console.log("The variable data types are not same");
}
Post a Comment