Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create Empty Values String in JavaScript?
In JavaScript, there are several ways to create empty string values. An empty string is a string with zero characters, represented by two quotation marks with nothing between them.
Syntax
let emptyString = ""; // Double quotes let emptyString = ''; // Single quotes let emptyString = ``; // Template literals let emptyString = String(); // String constructor
Example: Creating Empty Strings
<html>
<head>
<script>
var val1 = "";
var val2 = '';
var val3 = String();
document.write("Value 1: '" + val1 + "'<br>");
document.write("Type: " + typeof val1 + "<br><br>");
document.write("Value 2: '" + val2 + "'<br>");
document.write("Type: " + typeof val2 + "<br><br>");
document.write("Value 3: '" + val3 + "'<br>");
document.write("Type: " + typeof val3 + "<br><br>");
document.write("Are they equal? " + (val1 === val2));
</script>
</head>
<body>
</body>
</html>
Output
Value 1: '' Type: string Value 2: '' Type: string Value 3: '' Type: string Are they equal? true
Checking for Empty Strings
<html>
<head>
<script>
var emptyStr = "";
document.write("Length: " + emptyStr.length + "<br>");
document.write("Is empty? " + (emptyStr === "") + "<br>");
document.write("Is falsy? " + (!emptyStr) + "<br>");
document.write("Using length: " + (emptyStr.length === 0));
</script>
</head>
<body>
</body>
</html>
Output
Length: 0 Is empty? true Is falsy? true Using length: true
Comparison of Methods
| Method | Syntax | Use Case |
|---|---|---|
| Double quotes | "" |
Most common |
| Single quotes | '' |
Alternative style |
| Template literals | `` |
For template strings |
| String constructor | String() |
Explicit conversion |
Conclusion
Empty strings in JavaScript are created using "" or ''. They have a length of 0 and are considered falsy values, making them useful for conditional checks and string initialization.
Advertisements
