

Using the Array.filter() method, we filter out all the non-empty elements in the Array to a new array. This is valid in JavaScript, but might not produce correct output when operations are performed on it. Notice the empty string and empty element.

Here is an example of such array, var array = Say, you provide an empty element in the array it might return runtime errors if the operations performed on it are invalid. This type of feature might give the developer the freedom to code the way they like, but would put the system to test if something goes out of the line. This is valid is JavaScript, var array = With arrays in JavaScript, we can create heterogeneous collection of data. So, it better to be cautious from your side and write code that goes with standards. The loose type checking might sometimes give unexpected results that would halt our application or even crash. This sounds a good feature, but it is mostly bad. Submitted by Abhishek Pathak, on October 15, 2017 It is returning 1 as the string found in the array.Ĭode example 2: Using Array.findIndex() and toUpperCase() functions const subjects = Ĭonst result = subjects.findIndex(item => sub.toUpperCase() = item.toUpperCase()) Ĭonsole.log("String exists in the array") Ĭonsole.In this article, we will write a program to check if an array element is empty or not in JavaScript and make sure we have a consistent array of data. Using shows.findIndex() and lowercase() function we are ignoring the case and find the string in the array and assigning the returned value to the result variable.

In the above code example, we have created an array named shows that contain multiple items. Use Array.findIndex() function and inside this function convert the string to lowercase along with the converting array item to lowercase.Ĭode example 1: Using Array.findIndex() and String.toLowerCase() functions const shows = Ĭonst result = shows.findIndex( item => show.toLowerCase() = item.toLowerCase()).Create an array that contains multiple string items.
