Tech Junkie Blog - Real World Tutorials, Happy Coding!: JavaScript Array Methods: some() Method

Friday, December 31, 2021

JavaScript Array Methods: some() Method

The some() array works similar to the every() method, it also returns true or false when the condition is met.  some() needs to match just one element in the array that meets the condition to return true.  So lets run the same code that we ran for the every() method.  We will test to see if the array is greater than 50 for the first condition, and then we will test to see if the array is greater than 70 for the second condition.  For the every() method the first condition returns true, while the second condition returns false.  Let's see what happens with the some() method.


    <script>
        var oilPrices = [70.15, 69.50, 71.23, 74.32, 76.99];

        var greaterThanFifty = oilPrices.some(function (value) { return value > 50; });

        var greaterThanSeventy = oilPrices.some(function (value) { return value > 70 });

        console.log("greaterThanFifty: " + greaterThanFifty);
        console.log("greaterThanSeventy: " + greaterThanSeventy);
    </script>









As you can see both conditions return true because at least one of the element in the array meets the condition.


5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you so much for sharing very informative blog. I got more knowledge from this blog And i learn about java script from this blog anyone want learn java with data science course in Mumbai just click here

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Nice blog! Thanks for sharing such informative blog , As i am a student and doing my online training from power bi course in kolkata. Really awesome to have such course provider .

    ReplyDelete
  5. Adds one or more elements to the end of an array and returns the new length of the array.SEO Sohug

    ReplyDelete

Search This Blog