How to pass value to execute multiple conditions in JavaScript ?
Last Updated :
08 Jul, 2022
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....!!
Similar Reads
How to Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla
2 min read
How to Set Multiple Conditions in an If Statement in JavaScript? In JavaScript, conditional statements like if are used to execute code blocks based on certain conditions. There are situations where we might want to check multiple conditions within a single if statement. In such cases, JavaScript provides logical operators like && (AND) and || (OR) to com
5 min read
Alternative to Multiple OR Operators in JavaScript In this article, we will learn the alternatives to the use of multiple || (OR operator) in conditional statements. Multiple OR operators in conditional statements might cause poor readability of code and are often limited in functionality. Example: The below JavaScript code could be rewritten to per
4 min read
How to Use Dynamic Variable Names in JavaScript? Dynamic variable names are variable names that are not predefined but are generated dynamically during the execution of a program. This means the name of a variable can be determined at runtime, rather than being explicitly written in the code.Here are different ways to use dynamic variables in Java
2 min read
JavaScript Course Conditional Operator in JavaScript JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. if(expression){ do this; } The above argument named 'expression' is basically a condition that we pass into the 'if' and if it returns 'true' then the
3 min read
How to convert Number to Boolean in JavaScript ? We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer â0â or â1â into Boolean Value i.e. "false" or "true". Below are
2 min read
How to declare the optional function parameters in JavaScript ? Declaring optional function parameters in JavaScript means defining function parameters that aren't required when the function is called. You can assign default values to these parameters using the = syntax, so if no argument is provided, the default value is used instead. These are the following ap
3 min read
JavaScript - How to Use a Variable in Regular Expression? To dynamically create a regular expression (RegExp) in JavaScript using variables, you can use the RegExp constructor. Here are the various ways to use a variable in Regular Expression.1. Using the RegExp Constructor with a VariableIn JavaScript, regular expressions can be created dynamically using
3 min read
Less Than or Equal(<=) Comparison Operator in JavaScript JavaScript Less Than or Equal(<=) to the operator is used to compare two operands and return true if the left operand is smaller or equal to the right operand. The algorithm used for the comparison is the same as that of less than operator but equal condition is also checked Syntax: a<=b Examp
1 min read
How to use multiple ternary operators in a single statement in JavaScript ? In JavaScript, the ternary operator (? :) is a shorthand for if-else statements. You can use multiple ternary operators in a single statement to handle multiple conditions efficiently. This makes your code shorter, but be cautious as too many nested ternary operators can reduce code readability.Synt
2 min read