Check if Array contains Empty String in JavaScript-Code Examples

Radu TM

Check if Array contains Empty String in JavaScript

JavaScript by Radu TM June 21, 2022

const arr = ['a', 'b', 'c', ''];

// loop through arr
for (let i = 0; i < arr.length; i++) {
  // if the current element is an empty string
  if (arr[i] === '') {
    // print a message to the console
    console.log('The array contains an empty string.');
    // stop the loop
    break;
  }
}

0

20.900

Check if Array contains Empty String in JavaScript

JavaScript by Radu TM June 21, 2022

const checkForEmptyString = (arr) => {
  // return true if any element in arr is an empty string
  return arr.some((element) => {
    return element === "";
  });
};

0

20.900

Check if Array contains Empty String in JavaScript

JavaScript by Radu TM June 21, 2022

const arr = ['', 'not empty', 'also not empty']

arr.forEach(element => {
  if (element === '') {
    console.log('empty string found')
  }
})

0

20.900

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories