Home >Web Front-end >JS Tutorial >How to get the first non-null/undefined argument in JavaScript?
In JavaScript, many times we need to find the first non-null/undefined parameter in a function. This can be a tricky task, but luckily there are ways to help us accomplish this
A method is available to get the JavaScript The first non-null/undefined parameter is the Array.prototype.find() method. This method returns the value of the first element in the array that passes the given test. In our case, we can use this method to find the first non-null/undefined parameter by testing whether the parameter is not null/undefined.
In the following example, we define a function to find the first argument that is not empty or undefined.
<html> <head> <title>Example: finding first non-null arguements</title> </head> <body> <h2> Array.prototype.find() Method</h2> <div id="arguments"></div> <div id="result"></div> <script> function findFirstNonNullArgument(...args) { return args.find(arg => arg != null); } var result = findFirstNonNullArgument(null, undefined, "Hello", "World"); document.getElementById("arguments").innerHTML = `Arguments: null, undefined, "Hello", "World"`; document.getElementById("result").innerHTML = "<br>First Non-Null Argument: " + result; </script> </body> </html>
We can see from the above code that the findFirstNonNullArgument() function accepts a variable number of parameters and uses the find() method to return the first non-null/undefined parameter.
Another method that can be used to get the first non-null/undefined argument in JavaScript is Array.prototype.filter() method. This method creates a new array containing all elements that pass the given test. In our case, we can use this method to find the first non-null/undefined argument by passing in a test that checks if the argument is not null/undefined.
Below is the complete working code.
<html> <head> <title>Example: finding first non-null arguements</title> </head> <body> <h2> Array.prototype.filter() Method</h2> <div id="arguments"></div> <div id="result"></div> <script> function findFirstNonNullArgument(...args) { return args.filter(arg => arg != null)[0]; } document.getElementById("arguments").innerHTML = `Arguments: null, undefined, "Hello", "World"`; document.getElementById("result").innerHTML = "<br>First non-nul argument: " + findFirstNonNullArgument(null, undefined, "Hello", "World") </script> </body> </html>
From the above code we can see that the findFirstNonNullArgument() function accepts a variable number of parameters and uses the filter() method to return the first non-null/undefined parameter.
Another method that can be used to get the first non-null/undefined parameter in JavaScript is to use a for loop. This method loops through all parameters and checks if each parameter is not empty/undefined. If it finds a parameter that is not empty/undefined, it will return that parameter.
Below is the complete working code -
<html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> function findFirstNonNullArgument(...args) { for (let arg of args) { if (arg != null) { return arg; } } } document.getElementById("result").innerHTML = findFirstNonNullArgument(null, undefined, "Hello", "World") </script> </body> </html>
As we can see from the above code, the findFirstNonNullArgument() function accepts A variable number of arguments and using a for loop to return the first non-null/undefined argument.
In this article, we looked at three different methods you can use to get the first non-null/undefined parameter in JavaScript. Each method has its own advantages and disadvantages, so it's important to choose the right method for the task at hand.
The above is the detailed content of How to get the first non-null/undefined argument in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!