Simple way to condense if statements into a function to check arguments
Last Updated :
23 Aug, 2022
In this article, we will try to understand how we use if statements inside a function in order to check the validity of the argument, and later we will see the simple way to condense(reduce) if statements into a function to check arguments as well as theoretical explanations in JavaScript.
Let us first have a look into the following syntaxes (of object creation as well as method or function creation) which we will be using in our later shown examples:
Syntax to create an object in JavaScript: The following shown syntax is the syntax that we will use for object creation in JavaScript:
Syntax:
let object_name = {
property_nane : value,
...
}
Syntax to declare a function in JavaScript: The syntax (shown below is the syntax that is used for the method or function creation) in JavaScript (arrow function syntax declaration). The below syntax is used to declare the array functions in JavaScript.
Syntax:
let function_name = () => {
// do something...
// Either return some statement
// Or either use console.log()
}
Now let us have a look over the following section of examples in which we will visualize the actual use of the above-shown syntaxes as well as our problem statement too.
Example 1: In this example, we will make use of all the syntaxes shown in the above section and will also see how we may use multiple if-statements inside any function in order to check the argument's validity.
JavaScript
<script>
let checkArguments = (object) => {
if (object.color === "Green") {
console.log(`${object.name} is
of ${object.color} color`);
} else {
console.log("Invalid color...");
}
if (object.specifications.quantity < 10) {
console.log(`${object.name} has been
bought in very less quantity`);
} else {
console.log(`Right amount of
${object.name} has been bought...`);
}
if (!object.specifications.sweet) {
console.log(`Bad taste of ${object.name}
has been carried along...`);
} else {
console.log(`Good taste of ${object.name}
has been carried along`);
}
};
let fruit_object = {
name: "Grapes",
color: "Green",
specifications: {
quantity: 10,
price: 100,
sweet: true,
},
};
checkArguments(fruit_object);
</script>
Output:
Grapes is of Green color
Right amount of Grapes has been bought...
Good taste of Grapes has been carried along
The problem in the above approach: Now as we may visualize in the previous example that we have used multiple if-statements which makes our code a bit lengthier or less readable, so in another example, we will see the condensed form of if-statement which is by using the ternary operator to shorten the if-statement syntax.
Example 2: In this example, we will use the ternary operator instead of if-statements in order to reduce the overhead of a little long and less readable syntax. Ternary operator syntax will shorten the syntax and improves readability to the user to some extent.
JavaScript
<script>
let checkArguments = (object) => {
object.color === "Green"
? console.log(`${object.name} is
of ${object.color} color`)
: console.log("Invalid color...");
object.specifications.quantity < 10
? console.log(`${object.name} has
been bought in very less quantity`)
: console.log(`Right amount of
${object.name} has been bought...`);
!object.specifications.sweet
? console.log(`Bad taste of ${object.name}
has been carried along...`)
: console.log(`Good taste of ${object.name}
has been carried along`);
};
let fruit_object = {
name: "Grapes",
color: "Green",
specifications: {
quantity: 10,
price: 100,
sweet: true,
},
};
checkArguments(fruit_object);
</script>
Output:
Grapes is of Green color
Right amount of Grapes has been bought...
Good taste of Grapes has been carried along
Similar Reads
How to bind arguments to given values in Python functions? In Python, binding arguments to specific values can be a powerful tool, allowing you to set default values for function parameters, create specialized versions of functions, or partially apply a function to a set of arguments. This technique is commonly known as "partial function application" and ca
3 min read
Shell Script to Check if Every Passed Argument is a File or Directory While automating things in Linux we always work on the file and directories. And sometimes we pass the files and directories as the arguments to the shell scripts. And we have to determine whether the provided argument is a file or a directory, and today we are going to see how to check whether prov
4 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
How to use if, else & elif in Python Lambda Functions Lambda function can have multiple parameters but have only one expression. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to use if, else & elif in Lambda Functions.Using if-else in lambda functionThe lamb
2 min read
How to use If-Else Statement in Excel VBA? VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. In this article, we are going to learn how to use the If Else statement in Excel VBA. Impl
3 min read
How to Use AND Statement in if with JavaScript? In JavaScript AND (&&) logical operator is used in the 'if' statement to check for two or more conditional validities. AND logical operator in the 'if' condition returns true only when all the conditions inside the 'if' statement are TRUE. If any one condition inside the 'if' statement is FA
2 min read