Home > Web Front-end > JS Tutorial > How to get the first non-null/undefined argument in JavaScript?

How to get the first non-null/undefined argument in JavaScript?

PHPz
Release: 2023-08-24 20:17:12
forward
888 people have browsed it

如何在 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

Using Array.prototype.find()

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.

Example

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>
Copy after login

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. < /p>

Using Array.prototype.filter()

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.

Example

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>
Copy after login

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.

Using a for loop

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.

Example

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>
Copy after login

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.

Conclusion

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template