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
JavaScript: How to Check if a String is a Literal or an Object?
In this article, we will explore how JavaScript strings can exist as literals or objects, and learn methods to distinguish between them. In JavaScript, strings can exist in two forms:
- String Literal: Created using quotes (', ", or `)
-
String Object: Created explicitly using the String constructor with
new String("text")
Understanding String Literals vs String Objects
String literals are primitive values, while String objects are wrapper objects around the primitive string value. This distinction affects how they behave with certain operators and methods.
Using typeof to Check String Type
The typeof operator returns "string" for string literals and "object" for String objects:
<!DOCTYPE html>
<html lang="en">
<head>
<title>String Type Check with typeof</title>
</head>
<body>
<h1>String Type Detection</h1>
<script>
let literal = "Hello World";
let object = new String("Hello World");
console.log("String literal:", typeof literal);
console.log("String object:", typeof object);
document.write("<p>String literal type: " + typeof literal + "</p>");
document.write("<p>String object type: " + typeof object + "</p>");
</script>
</body>
</html>
String literal: string String object: object
Using instanceof to Check String Objects
The instanceof operator checks if an object was created by a specific constructor. It returns true for String objects and false for string literals:
<!DOCTYPE html>
<html lang="en">
<head>
<title>String Type Check with instanceof</title>
</head>
<body>
<h1>instanceof Check</h1>
<script>
let literal = "JavaScript";
let object = new String("JavaScript");
console.log("literal instanceof String:", literal instanceof String);
console.log("object instanceof String:", object instanceof String);
document.write("<p>Literal instanceof String: " + (literal instanceof String) + "</p>");
document.write("<p>Object instanceof String: " + (object instanceof String) + "</p>");
</script>
</body>
</html>
literal instanceof String: false object instanceof String: true
Complete Function to Identify String Type
Here's a comprehensive function that combines both approaches to identify whether a value is a string literal, String object, or another type:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Complete String Type Checker</title>
</head>
<body>
<h1>String Type Identification</h1>
<script>
function checkStringType(str) {
if (str instanceof String) {
return "String object";
} else if (typeof str === "string") {
return "String literal";
} else {
return "Not a string";
}
}
// Test with different values
let literal = "Hello Tutorials Point";
let object = new String("Hello Tutorials Point");
let number = 123;
let array = ["test"];
console.log(checkStringType(literal));
console.log(checkStringType(object));
console.log(checkStringType(number));
console.log(checkStringType(array));
document.write("<p>'" + literal + "' is: " + checkStringType(literal) + "</p>");
document.write("<p>String object is: " + checkStringType(object) + "</p>");
document.write("<p>Number 123 is: " + checkStringType(number) + "</p>");
document.write("<p>Array is: " + checkStringType(array) + "</p>");
</script>
</body>
</html>
String literal String object Not a string Not a string
Comparison: typeof vs instanceof
| Feature | typeof | instanceof |
|---|---|---|
| Purpose | Checks the primitive type | Checks constructor relationship |
| String Literal | "string" | false |
| String Object | "object" | true |
| Best Use Case | Detecting primitives | Detecting wrapper objects |
Key Points
- String literals are primitive values and more commonly used
- String objects are wrapper objects created with
new String() - Use
typeofto identify string literals - Use
instanceof Stringto identify String objects - Both types can use the same string methods, but behave differently with strict equality
Conclusion
Use typeof str === "string" to check for string literals and str instanceof String to check for String objects. Combining both approaches provides complete string type detection in JavaScript.
