How to check empty string in Lodash?

Member

by dasia , in category: JavaScript , 3 years ago

How to check empty string in Lodash?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by madie.koelpin , 2 years ago

@dasia You can use the isEmpty() method in Lodash to check for an empty string. Code:


1
2
3
4
5
6
7
8
import isEmpty from "lodash/isEmpty";

let str = "";

if (isEmpty(str)) {
  console.log("String is empty");
}
// Output: String is empty

Member

by lucile , 2 years ago

@dasia 

There are several ways to check for an empty string in Lodash:

  1. Using the _.isEmpty() method:
1
2
3
4
const str = '';
if (_.isEmpty(str)) {
  console.log('String is empty');
}


  1. Using the _.isString() and _.isEmpty() methods together:
1
2
3
4
const str = '';
if (_.isString(str) && _.isEmpty(str)) {
  console.log('String is empty');
}


  1. Using the JavaScript native method:
1
2
3
4
const str = '';
if (str === '') {
  console.log('String is empty');
}


All of these methods will produce the same result and can be used interchangeably based on personal preference.

Related Threads:

How to check empty if object is empty in Lodash?
How to check empty array in Lodash?
How to check if the string is empty in python?
How to check for an empty string in JTextField?
How to check empty string in golang?