
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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
- Related Articles
- How to check empty/undefined/null strings in JavaScript?
- Can MySQL automatically convert empty strings to NULL?
- How do I check for null values in JavaScript?
- How can I check if a JavaScript function is defined?
- Removing duplicates and inserting empty strings in JavaScript
- How do I empty an array in JavaScript?
- How can I convert an array to an object by splitting strings? JavaScript
- How can I check if a JavaScript variable is function type?
- How can I check whether a variable is defined in JavaScript?
- How can I concatenate two arrays in java
- How can I extend typed arrays in Swift?
- How can I convert Python strings into tuple?
- Finding the intersection of arrays of strings - JavaScript
- How to check if an object is empty using JavaScript?
- Check if value is empty in JavaScript

Advertisements