How to check empty string in JavaScript7 Jan 2025 | 4 min read In JavaScript, we can check whether a string is empty or not using several methods. Here are a few examples: 1. Using the length property:We can use the length property of a string to check whether it has any characters or not. If the length is zero, it means the string is empty. For example: Output String is empty 2. Using the trim() method:The trim() method removes whitespace from both ends of a string. If the string is empty, trim() will return an empty string. Output String is empty 3. Using strict equality comparison with an empty string:In JavaScript, an empty string is considered falsy, which means it is treated as false in a Boolean context. So, we can use strict equality comparison to check if the string is equal to an empty string. Output String is empty 4. Using ! operator:We can use the ! operator to check if the string is falsy. An empty string is considered falsy in JavaScript, so if the string is empty, ! will return true. Output String is empty 5. Using charAt() method:The charAt() method returns the character at a specified index in a string. If the string is empty, charAt() will return an empty string. Output String is empty 6. Using regular expressions:We can also use regular expressions to check for an empty string. The following regular expression matches an empty string: Output String is empty In this regular expression, ^ matches the start of the string, \s* matches zero or more whitespace characters, and $ matches the end of the string. If the string is empty or contains only whitespace characters, the regular expression will match and return true. 7. Using the Object.prototype.toString() method:If you have a variable that could be a string or another type of object, you can use the Object.prototype.toString() method to get its type and then check if it is a string and if it is empty. Output String is empty This code uses the Object.prototype.toString() method to get the type of the variable str, and then checks if it is a string by comparing the result to the string "[object String]". If it is a string, it trims the string and checks if it is empty. 8. Using the toString() method:If we have a variable that could be a string or null or undefined, we can use the toString() method to convert it to a string and then check if it is empty. Output String is empty This code first checks if the variable str is not null or undefined, and then converts it to a string using the toString() method. After that, it trims the resulting string and checks if it is empty. 9. Using the reduce() method:If we have an array of strings and we want to check if any of them are empty, we can use the reduce() method to iterate over the array and check if any of the strings are empty. Output Array contains an empty string This code uses the reduce() method to iterate over the array arr and check if any of the strings in the array are empty. The reduce() method takes a callback function that is called on each element of the array, and a starting value of false. The callback function checks if the current element is an empty string and returns true if it is, or the accumulator value (acc) otherwise. If any element in the array is an empty string, the final result of the reduce() method will be true, indicating that the array contains an empty string. Next TopicHow to flatten an array in javascript |
A comma-separated valuation (CSV) file is a demarcated file format that uses a comma to isolate the values. The data record is comprised of one or more than one field, separated by the commas. The name root for this file format is the comma-separated file...
5 min read
What is Form in JavaScript? In JavaScript, a form is an HTML element used to collect user input. Forms consist of one or more input elements and a submit button used to submit the form's data to a web server. If you want to create a...
6 min read
We will understand the in this article. How to define and access an object in JavaScript? We can define the JavaScript Object in the following ways: let keyword const keyword Utilizing let keyword We will utilize the let keyword and create the object using curly braces. Creating the object using the...
3 min read
? Perhaps you're curious about how a website appears with or without JavaScript. JavaScript is enabled by default on Chrome, but you can quickly disable it to observe how it affects the functionality and appearance of a website. Why is JavaScript disabled? You might want to ent JavaScript...
4 min read
. In this article, we will understand how to create Auto calculate price form in JavaScript. In this, we will learn some basics of this concept and some examples of Autocomplete form with the help of JavaScript. What do you mean by JavaScript? JavaScript abbreviated as JS. It...
6 min read
What is Babel.js? Babel.js commonly referred to as Babel is a toolchain that is mainly used to convert ECMAScript (ES6+) code into a backwards-compatible version of JavaScript that can be run by older JavaScript engines. It enables developers to use the JavaScript features without having to worry...
6 min read
A key component of web development is JavaScript which is well-known for its adaptability and dynamic quality. Understanding the pass by value and pass by reference procedures that are utilized to pass variables across functions is a basic component of programming in JavaScript. Developers may...
6 min read
JavaScript is a versatile and powerful programming language primarily used for web development. Developed by Brendan Eich in 1995, JavaScript was initially created to add interactivity to static web pages. Over the years, it has evolved into a key component of modern web development, enabling developers...
8 min read
What is an Array? In JavaScript, an array is a type of data structure that we use to store a collection of elements that can be of different types. These data types can include strings, numbers, Booleans (true or false), other arrays and objects. Example: const names =...
4 min read
JavaScript compilers are crucial in contemporary software development, transforming the execution of JavaScript code and enhancing performance. A JavaScript compiler converts JavaScript code from a readable form into machine code for web browsers or runtime environments. In contrast to interpreters, compilers analyze and translate complete scripts...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India