![]() So you may want to rewrite your code as follows: function contains(a, obj) )) //falseĬonsole.log(. Take the following example: var arr 'hello', 2, 4, 1, 2 console.log ( arr.includes ('hello') ) console.log ( arr.includes (2) ) console.log ( arr.includes (3) ) console.log ( arr. If you need the best performance, benchmark for yourself in the relevant execution environments.Īs others have said, the iteration through the array is probably the best way, but it has been proven that a decreasing while loop is the fastest way to iterate in JavaScript. The javascript includes function can be used to find if an element is present in an array. If you need an easy solution, look for other answers. ![]() ![]() The for loop is the most common way to loop through an array in JavaScript. There are several ways to loop through an array in JavaScript. It might not be relevant to modern JS execution contexts. One of the most important features of arrays in JavaScript is the ability to loop through the elements of an array. The promised performance improvement was based on a benchmark done in browsers of that time. It returns true if an array contains the element. Update from 2019: This answer is from 2008 (11 years old!) and is not relevant for modern JS usage. The JavaScript array includes() method checks whether the given array contains the specified element.
0 Comments
Leave a Reply. |