[Solved-5 Solutions] Check for an empty string in javascript - javascript tutorial
Problem:
Check for an empty string in javascript
Solution 1:
If we need to check whether there's any value, we can do
If we want to check specifically for an empty string over null, We would think checking against ""
is our best bet, using the ===
operator.
Read Also
Checking if a Key exist in a JavascriptSolution 2:
To check whether a string is empty, null or undefined we can use:
To check whether a string is blank, null or undefined we can use:
To check whether a string is blank or contains only white-space:
Solution 3:
We can use !!(not not) operator.
or use type casting:
- Both do the same function, type cast the variable to boolean, where str is a variable.
- Returns false for null,undefined,0,000,"",false.
- Returns true for string "0" and whitespace " ".
Read Also
Convert string to Boolean in JavascriptSolution 4:
If we want to ensure that the string is not just a group of empty spaces (In our imagining this is for form validation) we need to do a replace on the spaces.
Solution 5:
In this solution to check for an str.Empty,