Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to Check for an Empty String in JavaScript by String Comparison

// checking if a string is empty is by comparing the string to an empty string.

// For example:

let myStr = "";

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

// if we have white spaces, this will not read the string as empty.
// So we must first use the trim() method to remove all forms of whitespace:

let myStr = "   ";

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

// we can also check for the type of the value so that this will only run when
// the value is a string:

let myStr = null;

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

PREVIOUS NEXT
Code Example
Javascript :: decapitalize javascript string 
Javascript :: how to get the value of AutoCompelet Component in MUI 
Javascript :: Highest Scoring Word 
Javascript :: js addeventlistener input search phone 
Javascript :: expressjs allow cors for all hosts and ports 
Javascript :: javascript conditional 
Javascript :: nodejs mysql error handling with user example 
Javascript :: transition scrolling 
Javascript :: react function runs several times 
Javascript :: JSON requests using API in Javascript 
Javascript :: javascript filter array return index 
Javascript :: what is angularjs 
Javascript :: cypress/react yarn 
Javascript :: daysjs 
Javascript :: prettier overrides 
Javascript :: deserialize json to c# object 
Javascript :: javascript Sum of all the factors of a number 
Javascript :: javascript array de imagenes 
Javascript :: textinput onpress react native 
Javascript :: how to filter multiple values from a json api 
Javascript :: add material angular 
Javascript :: js not startswith 
Javascript :: Using An Array As A Parameter Of A Function 
Javascript :: discord.js reading json object from json 
Javascript :: for each loop in javascript 
Javascript :: chunking array javascript 
Javascript :: toast notification angular bootstrap 8 
Javascript :: react router hooks 
Javascript :: vscode format - .prettierrc jsx singleQuote not work 
Javascript :: json parse 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =