How to Check empty/undefined/null String in JavaScript? Last Updated : 17 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. Here are different approaches to check a string is empty or not.1. Using === OperatorUsing === operator we will check the string is empty or not. If empty then it will return "Empty String" and if the string is not empty it will return "Not Empty String".Syntaxif (str === "") { console.log("Empty String")} else { console.log("Not Empty String")} JavaScript // Function to check string is empty or not function checking(str){ // Checking the string using === operator if (str === "") { console.log("Empty String") } else { console.log("Not Empty String") } } // Checking for empty string checking(""); checking("GeeksforGeeks"); OutputEmpty String Not Empty String 2. Using length and ! OperatorThis approach uses length property to get the length of string and ! operator. and by using ! operator we will check string is empty or not. JavaScript // Function to check string is empty or not function checking(str) { return (!str || str.length === 0 ); } console.log(checking("")); console.log(checking("GeeksforGeeks")); Outputtrue false 3. Using replace() MethodThis approach uses replace() Method. It will ensure that the string is not just a group of empty spaces where we are doing replacement on the spaces. JavaScript // function to check string is empty or not function checking(str) { if(str.replace(/\s/g,"") == "") { console.log("Empty String") } else{ console.log("Not Empty String") } } checking(" "); checking("Hello Javascript"); OutputEmpty String Not Empty String Comment More infoAdvertise with us Next Article How to Check empty/undefined/null String in JavaScript? A AmanAgarwal6 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to check for "undefined" value in JavaScript ? In JavaScript, undefined is a primitive value that represents the absence of a value or the uninitialized state of a variable. It's typically used to denote the absence of a meaningful value, such as when a variable has been declared but not assigned a value. It can also indicate the absence of a re 2 min read How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if 2 min read How to check for null values in JavaScript ? The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is becau 4 min read How to get the first non-null/undefined argument in JavaScript ? There are many occasions when we get to find out the first non-null or non-undefined argument passed to a function. This is known as coalescing. Approach 1: We can implement coalescing in pre-ES6 JavaScript by looping through the arguments and checking which of the arguments is equal to NULL. We the 5 min read JavaScript - How To Check if String Contains Only Digits? A string with only digits means it consists solely of numeric characters (0-9) and contains no other characters or symbols. Here are the different methods to check if the string contains only digits.1. Using Regular Expression (RegExp) with test() MethodThe most efficient way to check if a string co 3 min read How to Replace a value if null or undefined in JavaScript? In JavaScript, replacing a value if it is null or undefined involves providing a default value to use when the original value is either null or undefined. This ensures the application has valid data, preventing potential errors or unexpected behavior. Here we have some common approaches: Table of Co 2 min read How to check a date is valid or not using JavaScript? To check if a date is valid or not in JavaScript, we have to know all the valid formats of the date. For ex - "YYYY/DD/MM", "DD/MM/YYYY", and "YYYY-MM-DD", etc. We have a given date format and we need to check whether the given format is valid or not according to the official and acceptable date for 3 min read How to check for empty string in PHP ? In this article, we will see how to check for empty string in PHP. String is a set of characters. A string is said to be empty, if it contains no characters. We can use empty() function to check whether a string is empty or not.here we have some common approachesTable of ContentUsing empty() functio 4 min read How to check a string data type is present in array using JavaScript ? In JavaScript, an array is a collection of data that can be of the same or different type. If we have the array containing the data, our task is to identify if it is a string data type. In this article, we will learn how to use the typeof operator. Syntax: typeof value Note: The typeof operator retu 3 min read How to Detect an Undefined Object Property in JavaScript ? Detecting an undefined object property is the process of determining whether an object contains a certain property, and if it does, whether the value of that property is undefined. This is an important concept in JavaScript programming, as it helps to prevent errors that can occur when attempting to 3 min read Like