{"id":16554,"date":"2022-09-08T04:50:43","date_gmt":"2022-09-08T11:50:43","guid":{"rendered":"https:\/\/coderpad.io\/?p=16554"},"modified":"2023-06-05T14:10:21","modified_gmt":"2023-06-05T21:10:21","slug":"javascript-some-method","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/javascript-some-method\/","title":{"rendered":"JavaScript .some() Method: When and How to Use It"},"content":{"rendered":"\n<p>Arrays in JavaScript offer many built-in functionalities that many developers are unaware of, among these is the <code>.some()<\/code> method.<\/p>\n\n\n\n<p>The <code>.some()<\/code> method comes in handy when the developer needs to check for a condition on an array and wants to know if at least one element fulfills that condition. This condition is passed in as a function that runs for each element of the array and exits execution the moment a true value is returned from the function. You also have access to that particular element as one of the function arguments.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s understand the syntax of the <code>.some()<\/code> method.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">some(<span class=\"hljs-function\">(<span class=\"hljs-params\">element, index, array<\/span>) =&gt;<\/span> { <span class=\"hljs-comment\">\/* condition function returns boolean *\/<\/span> } )\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here we have the element in context (the element of the array). The <em>condition function<\/em> will run on each member of the array unless the array is exhausted or the condition function returns <code>true<\/code>. Let&#8217;s take a simple example to understand this better.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Understanding some() with an example<\/strong><\/h3>\n\n\n\n<p>Say we run an online direct-from-the-farm fruit delivery service. If a user has any item that costs more than $30.00 added to their cart, we&#8217;ll offer them free delivery. We can assume that the cart items in memory exist as per the following basic data structure:&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> itemsInCart = &#91;\n\u00a0\u00a0{ <span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-number\">123<\/span>, <span class=\"hljs-attr\">name<\/span>: \u201cApple\u201d, <span class=\"hljs-attr\">cost<\/span>: <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-attr\">quantity<\/span>: <span class=\"hljs-number\">6<\/span> },\n\u00a0\u00a0{ <span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-number\">456<\/span>, <span class=\"hljs-attr\">name<\/span>: \u201cCoconut\u201d, <span class=\"hljs-attr\">cost<\/span>: <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-attr\">quantity<\/span>: <span class=\"hljs-number\">1<\/span> },\n\u00a0\u00a0{ <span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-number\">789<\/span>, <span class=\"hljs-attr\">name<\/span>: \u201cOrange\u201d, <span class=\"hljs-attr\">cost<\/span>: <span class=\"hljs-number\">15<\/span>, <span class=\"hljs-attr\">quantity<\/span>: <span class=\"hljs-number\">2<\/span> }\n]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The first thing we&#8217;ll do is set a flag variable <code>isOver30<\/code> as <code>false <\/code>so that we can set it to <code>true <\/code>once our desired condition is fulfilled. We&#8217;ll then run a <code>.forEach()<\/code> loop on the cart items&#8217; array to keep looking for any item that costs more than $30. If we find an item that costs more than $30, the flag will be set to<code>true<\/code>, and the loop will be exited.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> isOver30= <span class=\"hljs-literal\">false<\/span>;\n\nitemsInCart.forEach(<span class=\"hljs-function\">(<span class=\"hljs-params\">item<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (item.cost &gt; <span class=\"hljs-number\">30<\/span>) {\n    isOver30= <span class=\"hljs-literal\">true<\/span>;\n    <span class=\"hljs-keyword\">return<\/span>;\n  }\n});\n\n<span class=\"hljs-comment\">\/\/ use the isOver30 flag to make further decisions<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Wait a minute. The <code>.forEach()<\/code>method does not let us exit the loop once done. Once the function passes the &#8220;coconut&#8221; object, our condition would be rendered<code>true<\/code>. Thus, we don&#8217;t need to continue anymore. But <code>.forEach<\/code> runs on all the elements of an array. This means it&#8217;s overkill in our case.&nbsp;<\/p>\n\n\n\n<p>Even return statements inside <code>.forEach()<\/code> act as continue statements\u2014not like break statements, which we require. Well, it&#8217;s quite easy to exit out of a good old <code>for<\/code> loop. So, let&#8217;s give it a shot:&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> isOver30= <span class=\"hljs-literal\">false<\/span>;\n<span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">let<\/span> index = <span class=\"hljs-number\">0<\/span>; index &lt; itemsInCart.length; index++) {\n  <span class=\"hljs-keyword\">const<\/span> item = itemsInCart&#91;index];\n  <span class=\"hljs-keyword\">if<\/span> (item.cost &gt; <span class=\"hljs-number\">30<\/span>) {\n    isOver30 = <span class=\"hljs-literal\">true<\/span>;\n    <span class=\"hljs-keyword\">break<\/span>;\n  }\n}\n<span class=\"hljs-comment\">\/\/ use the isOver30 flag to make further decisions<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>While this would suffice, it\u2019s not the most readable code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Making the code cleaner<\/strong><\/h3>\n\n\n\n<p>Now let us use the <code>.some()<\/code> method to take care of this logic in a cleaner manner.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> conditionFunc = <span class=\"hljs-function\"><span class=\"hljs-params\">item<\/span> =&gt;<\/span> item.cost &gt; <span class=\"hljs-number\">30<\/span>;\n<span class=\"hljs-keyword\">const<\/span> isEligibleForFreeDelivery = itemsInCart.some(conditionFunc);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Two lines \u2013that\u2019s all it takes. We\u2019ve cut our code length by almost 75% with the <code>.some()<\/code>method. We could technically even bring this down to one line, as our <code>conditionFunc<\/code> is a straightforward function.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> isEligibleForFreeDelivery = itemsInCart.some(<span class=\"hljs-function\">(<span class=\"hljs-params\">item<\/span>) =&gt;<\/span> item.cost &gt; <span class=\"hljs-number\">30<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>.some()<\/code>method stops execution once our condition function returns<code>true<\/code>. That means that <code>.some()<\/code> does not even operate on all the elements of an array, unlike methods like <code>.forEach()<\/code>. This makes <code>.some()<\/code> an ideal method when you don\u2019t want to iterate over every element of an array. Apart from the element itself, we also have access to the index and the whole array, as well as inside the <code>conditionFunc<\/code>. Developers can use this to solve their problems as they see fit.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding the difference between some() and every()<\/strong><\/h2>\n\n\n\n<p>The <code>.some()<\/code> method is built on the same foundation as that of the methods <code>.every()<\/code> and <code>.find()<\/code>. The <code>.every()<\/code> method returns<code>true <\/code>if every array element passes the provided condition, stops execution, and returns <code>false <\/code>the moment even one element does not fulfill the condition.&nbsp;<\/p>\n\n\n\n<p>On the other hand, the <code>.find()<\/code> method returns the first element that satisfies the provided condition. Because of this, it&#8217;s similar to the functionality of the <code>.some()<\/code> method.&nbsp;<\/p>\n\n\n\n<p>Of course, this could all technically be achieved with a <code>for<\/code> loop or in a custom manner. However, you&#8217;re also required to appropriately document and<a href=\"https:\/\/coderpad.io\/blog\/development\/what-you-never-learned-about-javascript-functions\/\">explain your functions<\/a> so that other developers can use them. You have to name them right, add tons of comments, and hope future devs understand what you&#8217;re talking about.&nbsp;<\/p>\n\n\n\n<p>You don\u2019t have to worry about that with standardized JavaScript functions like <code>.some()<\/code> and <code>.find()<\/code>. With these methods, it&#8217;s much easier to deduce the purpose of that particular block of code without understanding its actual logic. This is the reason it&#8217;s best practice to use a built-in function if there&#8217;s one for what a developer is trying to achieve.&nbsp;<\/p>\n\n\n\n<p>Just check out this code snippet to see for yourself:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> users = &#91;\n  { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"John\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">20<\/span> },\n  { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Sarah\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">19<\/span> },\n  { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Bob\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">30<\/span> },\n  { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Alice\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">25<\/span> },\n];\n\n<span class=\"hljs-comment\">\/\/ Using some() functions to check if any of the users are older than 20<\/span>\n<span class=\"hljs-keyword\">const<\/span> olderThan20 = users.some(<span class=\"hljs-function\">(<span class=\"hljs-params\">user<\/span>) =&gt;<\/span> user.age &gt; <span class=\"hljs-number\">20<\/span>);\n<span class=\"hljs-built_in\">console<\/span>.log(olderThan20);  <span class=\"hljs-comment\">\/\/ true<\/span>\n\n<span class=\"hljs-comment\">\/\/ Using every() function to check if all users are older than 20<\/span>\n<span class=\"hljs-keyword\">const<\/span> allOlderThan20 = users.every(<span class=\"hljs-function\">(<span class=\"hljs-params\">user<\/span>) =&gt;<\/span> user.age &gt; <span class=\"hljs-number\">20<\/span>);\n<span class=\"hljs-built_in\">console<\/span>.log(allOlderThan20); <span class=\"hljs-comment\">\/\/false<\/span>\n\n<span class=\"hljs-comment\">\/\/ Using find() function to find the first user older than 20<\/span>\n<span class=\"hljs-keyword\">const<\/span> firstOlderThan20 = users.find(<span class=\"hljs-function\">(<span class=\"hljs-params\">user<\/span>) =&gt;<\/span> user.age &gt; <span class=\"hljs-number\">20<\/span>);\n\n<span class=\"hljs-comment\">\/\/ Using find() function to find the first user older than 20 and return the name<\/span>\n<span class=\"hljs-keyword\">const<\/span> firstOlderThan20Name = users.find(<span class=\"hljs-function\">(<span class=\"hljs-params\">user<\/span>) =&gt;<\/span> user.age &gt; <span class=\"hljs-number\">20<\/span>).name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\"><strong>Best practices for using some() in JavaScript<\/strong><\/h2>\n\n\n\n<p>The advantage of using <code>.some()<\/code> over other methods for similar tasks is that the final code is cleaner than the conventional approaches we discussed. This will allow future devs to understand this code&#8217;s purpose and extend it accordingly.&nbsp;<\/p>\n\n\n\n<p>There are a few instances when we should stick to the <code>for<\/code> loops:&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>If the conditions are more complex than in our example above<\/li><li>If we need to operate on multiple arrays<\/li><li>If there are multiple exit conditions, which will then make your functions highly tailored to your problem and not a one-size-fits-all approach<\/li><\/ol>\n\n\n\n<p>It all depends on your use case. If you need to run condition checks on elements of an array and need at least one of them to be true, then <code>.some()<\/code> is the way to go.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Wrapping up<\/strong><\/h2>\n\n\n\n<p>JavaScript keeps coming up with practical built-in methods that offer ease and convenience for current and future developers of any given codebase, thus allowing everyone to build solutions rapidly. The <code>.some()<\/code> <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/some\" target=\"_blank\" rel=\"noopener\">array<\/a> is another method that solves a particular problem. It won&#8217;t be the solution to all our problems, but whenever it is, it can render a final code output that is extremely clean and high quality.&nbsp;<\/p>\n\n\n\n<p><em>This post was written by Keshav Malik. <\/em><a href=\"https:\/\/theinfosecguy.xyz\/\" target=\"_blank\" rel=\"noopener\"><em>Keshav<\/em><\/a><em> is a full-time developer who loves to build and break stuff. He is constantly looking for new and exciting technologies and enjoys working with diverse technologies in his spare time. He loves music and plays badminton whenever the opportunity presents itself.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript arrays offer lots of built-in functionality. Explore the usability of the .some() function, what it offers, and what it can achieve.<\/p>\n","protected":false},"author":12,"featured_media":16560,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[33],"keyword-cluster":[],"class_list":["post-16554","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/16554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=16554"}],"version-history":[{"count":66,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/16554\/revisions"}],"predecessor-version":[{"id":16713,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/16554\/revisions\/16713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/16560"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=16554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=16554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=16554"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=16554"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=16554"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=16554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}