Open In App

How to pass value to execute multiple conditions in JavaScript ?

Last Updated : 08 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will try to understand how we can pass certain values in multiple conditions in JavaScript with the help of certain coding examples.

The term, multiple conditionals, actually refer to more than one conditionals in number. This would eventually include the usage of “if-elseif-else” or switch statement. We can also use special functions available under JavaScript with which our values could be passed in multiple conditionals.

Let us first have a look over the below-illustrated syntaxes of some pre-available conditionals in JavaScript.

The following syntax is the syntax of “if-elseif-else”  which we may use in case of multiple conditions checking.

Syntax:

if(first_condition) {
    // do something...
}

else if(second_condition){
    // do something...
}

// More else-if's could be added depending upon the need
// At last one else part has to be added...

else{
    // do something...
}

The switch condition will eventually take multiple values inside separate written cases and at last one default case containing values has to be written.

Syntax:

switch(conditional_parameter) {
    case first_value :
        // do something...

    case second_value :
        // do something...

    // More cases depending upon need could be added...

    default :
        // do something...
}

Some methods, and operators (logical as well as simple ones) will help in our task to pass values inside multiple conditionals in JavaScript.

Example 1: In this example, we will create a function that will accept some value within it and we will check that value and as per the conditions and values, we will return some value as output.

Javascript




<script>
let checkTemperature = (value) => {
  if (value > 30) {
    return "Too Humid and Hot...!!";
  } else {
    return "It's too cold here....!!";
  }
};
 
console.log(checkTemperature(35));
console.log(checkTemperature(12));
</script>


Output:

Too Humid and Hot...!!
It's too cold here....!!

Example 2: In this example, we will use a switch case inside a function which will be responsible for checking values and returning values according to that only.

Javascript




<script>
let checkFruitViaColor = (color) => {
  switch (color) {
    case "green":
      return "Grapes";
 
    case "red":
      return ["apple", "pomegranate"];
 
    default:
      return "No fruit...";
  }
};
 
console.log("Green color fruit is: "
    + checkFruitViaColor("green"));
console.log("Red color fruit is: "
    + checkFruitViaColor("red"));
<script>


Output:

Green color fruit is: Grapes
Red color fruit is: apple,pomegranate

Example 3: In this example, we will use the includes() method along with logical AND-operator (&&) which will check for certain conditions. We will show output in the “if” statement depending on the case.

Javascript




<script>
let checkData = (name_of_organization) => {
  let check =
    name_of_organization.includes("Geeks") &&
    name_of_organization.includes("for") &&
    name_of_organization.includes("GeeksforGeeks");
 
  if (check === true) {
    return "All conditions passed successfully....!!";
  } else {
    return "Failed....!!";
  }
};
 
console.log(checkData("GeeksforGeeks"));
<script>


Output:

All conditions passed successfully....!!


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads