Open In App

Simple way to condense if statements into a function to check arguments

Last Updated : 23 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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


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

Similar Reads