6

How to Check for an Empty String in JavaScript

 2 years ago
source link: https://www.laravelcode.com/post/how-to-check-for-an-empty-string-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Check for an Empty String in JavaScript

  1062 views

  2 years ago

Javascript

Use the === Operator

You can use the strict equality operator (===) to check whether a string is empty or not.

The comparsion str === "" will only return true if the data type of the value is string and it is not empty, otherwise return false as demonstrated in the following example:

<script>
if(str === ""){
    // string is empty, do something
}
 
// Some test cases
alert(2 === "");  // Outputs: flase
alert(0 === "") // Outputs: false
alert("" === "") // Outputs: true
alert("Hello World!" === "") // Outputs: false 
alert(false === "") // Outputs: false
alert(null === "") // Outputs: false
alert(undefined === "") // Outputs: false
</script>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK