Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to Check for an Empty String in JavaScript with the length Property

// In this method, we will check for the length of the string by adding the length property.
// We'll check if the length is equal to 0. If it’s equal to zero,
// it means that the string is empty, as we can see below:

let myStr = "";

if (myStr.length === 0) {
  console.log("This is an empty string!");
}

/*
The above will return this:

"This is an empty string!"

But this approach unfortunately might not work in all situations.
For example, if we have a string that has white spaces as seen below:
*/


let myStr = "  ";

if (myStr.length === 0) {
  console.log("This is an empty string!");
}else{
  console.log("This is NOT an empty string!");
}

/*
This will return:

"This is NOT an empty string!"

We can easily fix this error by first removing the white spaces using the trim() method
before checking for the length of such string to see if it’s empty as seen below:
*/


let myStr = "  ";

if (myStr.trim().length === 0) {
  console.log("This is an empty string!");
}else{
  console.log("This is NOT an empty string!");
}

/*
This will now return the following:

"This is an empty string!"

Note: If the value is null, this will throw an error because the length property does
not work for null.

To fix this, we can add an argument that checks if the value's type is a string and
skips this check if it is not:
*/


let myStr = null;

if (typeof myStr === "string" && myStr.trim().length === 0) {
  console.log("This is an empty string!");
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Javascript code to Detect All Network Number In Nigeria (MTN, Glo, Airtel & 9Mobile). 
Javascript :: jquery check component exists 
Javascript :: import json file into javascript 
Javascript :: scss in react app 
Javascript :: password validation regex 
Javascript :: node js error 
Javascript :: nested ternary operator javascript 
Javascript :: jquery prev 
Javascript :: how to fill html datalist with array values in javascript 
Javascript :: react axios Card List 
Javascript :: HH:mm with am pm jquery 
Javascript :: mongoose cursor eachasync 
Javascript :: avoid compressing imagepicker react native 
Javascript :: random email js 
Javascript :: .includes javascript 
Javascript :: double function call javascript 
Javascript :: comparare due array di numeri javascript 
Javascript :: searchbar to bottom table datatable 
Javascript :: js array entries 
Javascript :: The data option must be a function. Plain object usage is no longer supported. vue 
Javascript :: for loop with if statement 
Javascript :: how to setup material-table in react 
Javascript :: reac native play sound 
Javascript :: javascript set elements width by tag name 
Javascript :: how to add row in angular dynamically 
Javascript :: readystate==4 
Javascript :: Jquery check if hover over child element 
Javascript :: js replace text link with anchor tags 
Javascript :: vue back image 
Javascript :: javascript upload file button 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =