What is the usage of every() method in JavaScript?

王林
Release: 2023-09-01 23:05:06
forward
681 people have browsed it

What is the usage of every() method in JavaScript?

JavaScript ArraysEach method tests whether all elements in the array pass the test implemented by the provided function.

The following are the parameters -

  • callback - the function used to test each element.
  • thisObject >− The object used as this when executing the callback.

Example

You can try running the following code to understand how to operate each () method in JavaScript -

<html>
   <head>
      <title>JavaScript Array every Method</title>
   </head>

   <body>
      <script>
         if (!Array.prototype.every) {
            Array.prototype.every = function(fun /*, thisp*/) {
               var len = this.length;
               
               if (typeof fun != "function")
               throw new TypeError();
               var thisp = arguments[1];
               
               for (var i = 0; i < len; i++) {
                  if (i in this && !fun.call(thisp, this[i], i, this))
                  return false;
               }
               return true;
            };
          }
          function isBigEnough(element, index, array) {
             return (element >= 10);
          }
          var passed = [12, 5, 8, 130, 44].every(isBigEnough);
          document.write("First Test Value : " + passed );
          passed = [12, 54, 18, 130, 44].every(isBigEnough);
          document.write("<br>Second Test Value : " + passed );
      </script>
   </body>
</html>
Copy after login

The above is the detailed content of What is the usage of every() method in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!