Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Check If a String is Empty in JavaScript</title> </head> <body> <script> var str = "Hi there!" if(str === ""){ // string is empty, do something alert("String is empty."); } else{ alert("String is not empty."); } // Some test cases console.log(2 === ""); // Outputs: flase console.log(0 === "") // Outputs: false console.log("" === "") // Outputs: true console.log("Hello World!" === "") // Outputs: false console.log(false === "") // Outputs: false console.log(null === "") // Outputs: false console.log(undefined === "") // Outputs: false </script> <p><strong>Note:</strong> Please check out the browser console by pressing the f12 key on the keyboard.</p> </body> </html>