Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How can I check JavaScript arrays for empty strings?
Let’s say the following is our array with non-empty and empty values −
studentDetails[2] = "Smith";
studentDetails[3] = "";
studentDetails[4] = "UK";
function arrayHasEmptyStrings(studentDetails) {
for (var index = 0; index < studentDetails.length; index++) {
To check arrays for empty strings, the syntax is as follows. Set such condition for checking −
if(yourArrayObjectName[yourCurrentIndexvalue]==””){
// insert your statement
} else{
// insert your statement
}
Example
var studentDetails = new Array();
studentDetails[0] = "John";
studentDetails[1] = "";
studentDetails[2] = "Smith";
studentDetails[3] = "";
studentDetails[4] = "UK";
function arrayHasEmptyStrings(studentDetails) {
for (var index = 0; index < studentDetails.length; index++) {
if (studentDetails[index] == "")
console.log("The array has empty strings at the index=" +
(index));
else
console.log("The value is at
index="+(index)+"="+studentDetails[index]);
}
}
arrayHasEmptyStrings(studentDetails);
To run the above program, you need to use the following command −
node fileName.js
Here my file name is demo210.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo210.js The value is at index=0=John The array has empty strings at the index=1 The value is at index=2=Smith The array has empty strings at the index=3 The value is at index=4=UK
Advertisements
