Snipps by Patrik

How to Check if a String is Empty in JavaScript?

Question: How can I check if a string is empty?

Option 1: Check using ""

if (str === "") {
}

Note: to know if it's an empty string use === instead of ==. This is because === will only return true if the values on both sides are of the same type, in this case a string. for example: (false == "") will return true, and (false === "") will return false.

Option 2: Check length of string

if (!str || str.trim().length === 0) {
}

Comments

Leave a Comment

All fields are required. Your email address will not be published.