Check empty/undefined/null string in JavaScript
In JavaScript, you can check for an empty string using various methods. An empty string is a string that contains no characters, not even whitespace.
Using the Length Property
You can use the .length property of a string to determine its length. An empty string will have a length of 0.
Using the Not Operator (!)
The not operator (!) can be used to convert a string to a Boolean value. An empty string is falsy, so using ! will return true for an empty string and false for a non-empty string.
Using String Comparison
You can directly compare the string with an empty string using equality (===) or inequality (!==) operators.
Trimming and Checking Length
If you want to consider strings containing only whitespace as empty, you can first trim the string to remove leading and trailing spaces, then check its length.
Keep in mind that these methods will help you determine if a string is completely empty. If you want to check if a string only contains whitespace, you'll need to use the trimming approach mentioned in method 4.
Difference between empty, null, undefined and NaN
- Empty String (""): An empty string is a sequence of characters with zero length. It contains no visible characters and is represented by a pair of double quotation marks with nothing in between.
- Null: A null value in JavaScript is an intentional absence of any value. It represents the intentional lack of value and is often used to signify that a variable or object property doesn't have a valid value.
- Undefined: In JavaScript, a variable that has been declared but hasn't been assigned a value yet holds the value undefined. It's the default value given to variables that are declared but not initialized.
- NaN (Not-a-Number): NaN is a special value that represents a value that is not a valid number. It results from operations that involve non-numeric values or undefined values that can't be interpreted as numbers.
According to ES specification, the following values will evaluate to false in a conditional context -
- empty string ("")
- null
- undefined
- NaN
This means that none of the following if statements will get executed:
- if ("") {}
- if (null) {}
- if (undefined) {}
- if (NaN) {}
Conclusion
You can check for an empty string in JavaScript by using methods like checking its length property (str.length === 0), using the not operator (!str), or comparing it directly to an empty string (str === ""). To also consider strings with only whitespace as empty, you can trim the string and then check its length (str.trim().length === 0).
- JavaScript Popup Boxes
- Opening a new window in JavaScript
- How to Create Drop-Down Lists in JavaScript
- How do I include a JavaScript file in another JavaScript file?
- Print the content of a Div using JavaScript/jQuery
- How to get the current URL using JavaScript ?
- How to Detect a Mobile Device with JavaScript/jQuery
- How to validate an email address in JavaScript
- JavaScript Array Iteration
- How to Remove a Specific Item from an Array in JavaScript
- What is JavaScript closures?
- How To Remove a Property from a JavaScript Object
- How to get selected value from Dropdown list in JavaScript
- How do I get the current date in JavaScript?
- How to Open URL in New Tab | JavaScript
- How to delay/wait/Sleep in code execution | JavaScript
- How to round to at most 2 decimal places | JavaScript
- How to convert string to boolean | JavaScript
- How to check undefined in JavaScript?
- How To Copy to Clipboard | JavaScript
- How to encode a URL using JavaScript?
- How to force Input field to enter numbers only | JavaScript
- How to create multiline string in JavaScript