Navigate back to the homepage

How to check if a string is empty or undefined in javascript?

Infinitbility
JavaScript
May 26th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show you how do you check if a string is empty or undefined in javascript, here I will use some common syntax to check string is empty, undefined, or have some value.

The javascript if... else statement auto handles these types of cases like null, undefined, NaN, empty string (""), 0, and false.

For all of these cases it will think falsy values, so to handle empty or undefined we have to just if... else statement.

Let’s start today’s tutorial How to check if a string is empty or undefined in javascript?

Here, we will take the example of an undefined, empty, and valid string value to check whether actually if... else statement can handle it or not.

Let’s plan out what we are going to do.

  1. create variables that store undefined, empty, and valid string value
  2. check the undefined value in the if...else statement
  3. check the empty value in the if...else statement
  4. check the string value in the if...else statement
1// create variables which store undefined, empty and valid string value
2const str1 = undefined;
3const str2 = "";
4const str3 = "infinitbility";
5
6// check undefined value in `if...else` statement
7if(str1){
8 console.log("str1 is a valid value");
9} else {
10 console.log("str1 is a invalid value");
11}
12
13// check empty value in `if...else` statement
14if(str2){
15 console.log("str2 is a valid value");
16} else {
17 console.log("str2 is a invalid value");
18}
19
20// check string value in `if...else` statement
21if(str3){
22 console.log("str3 is a valid value");
23} else {
24 console.log("str3 is a invalid value");
25}

When you run the above program it should console a valid string for only the str1 variable.

let’s check the output.

javascript, check if a string is empty or undefined example
javascript, check if a string is empty or undefined example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a string is a substring of another string in javascript?

Example of check if a string is a substring of another string in javascript with step by step guide.

May 25th, 2022 · 1 min read

How to check if a string is a valid json string in javascript?

Example of check if a string is a valid json string in javascript with step by step guide.

May 25th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]