Question #5   Submitted by Answiki on 04/27/2020 at 04:50:06 PM UTC

How to check for an empty JS string ?

Answer   Submitted by Answiki on 04/27/2020 at 06:25:28 PM UTC

The best way to check if a JavaScript string is empty (whether there's any value) is to use the following:

if (myString) {
    // Do something
}


The following alternative will return true only if the type of myString is a string and the string is empty.

if (myString === '') {
    // Do something
}

You may also want to check if a string is empty or contains only spaces.

5 events in history
Answer by Answiki on 04/27/2020 at 06:25:28 PM

The best way to check if a JavaScript string is empty (whether there's any value) is to use the following:

if (myString) {
    // Do something
}


The following alternative will return true only if the type of myString is a string and the string is empty.

if (myString === '') {
    // Do something
}

You may also want to check if a string is empty or contains only spaces.

Answer by Answiki on 04/27/2020 at 05:40:24 PM

The best way to check whether there's any value is to use the following:

if (myString) {
    // Do something
}


The following alternative will return true only if the type of myString is a string and the string is empty.

if (myString === '') {
    // Do something
}

You may also want to check if a string is empty or contains only spaces.

Question by Answiki 04/27/2020 at 04:50:06 PM
How to check for an empty JS string ?
Answer by Answiki on 04/27/2020 at 04:49:37 PM

The best way to check whether there's any value is to use the following:

if (myString) {
    // Do something
}


The following alternative will return true only if the type of myString is a string and the string is empty.

if (myString === '') {
    // Do something
}

Question by Answiki 04/27/2020 at 04:23:10 PM
How to check for an empty JavaScript string ?
# ID Query URL Count

Icons proudly provided by Friconix.