Check if Array contains Empty Elements in JavaScript-Code Examples

Radu TM

Check if Array contains Empty Elements in JavaScript

JavaScript by Radu TM June 21, 2022

// check if array contains empty elements
const checkIfArrayContainsEmptyElements = (arr) => {
  // iterate over each element in the array
  for (const elem of arr) {
    // check if element is an empty string
    if (elem === "") {
      // if so, return true
      return true;
    }
  }
  // if we get here, that means the array doesn't contain any empty elements
  return false;
}

// usage
const arr = ["🍎", "", "🍌", "🍇"];
console.log(checkIfArrayContainsEmptyElements(arr)); // true

0

21.136

Check if Array contains Empty Elements in JavaScript

JavaScript by Radu TM June 21, 2022

const arr = [1, 2, 3, 4, 5]

arr.forEach(element => {
  if (element === null || element === undefined || element === '') {
    console.log('There is an empty element in the array!')
  }
})

0

21.136

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories