JavaScript: How to Check if a String is a Literal or an Object?



In this article, we will be exploring strings and how they can be used as a literal and an object depending on the requirements. In JavaScript, strings can exist in two forms:

String Literal: Created using quotes (', ", or `).
String Object: Created explicitly using the String constructor, e.g., new String("text").

JavaScript Literals

A JavaScript literal can be referred to as representing fixed values in source code. In most languages, values are denoted by Integers, floating-point numbers, strings, boolean, characters, arrays, records, and many more.

JavaScript Objects

On the other hand, a JavaScript object can be defined as a list of unordered primitive data types (and sometimes reference data types) that are stored as a pair with key and value. In this list, each item is defined as a property.

The type of Operator

Now how we will check whether the string is a literal or an object.

For this, we will be using the typeof operator. The typeof operator returns the type of any data type in JavaScript and returns their actual data type. Operands can either be literals or data structures such as variables, functions, or objects. An operator returns the type of data.

We can also use the instanceof operator to compare an instance with an Object. It will return the instance of the particular object.

Checking if a String is Literal or Object

JavaScript provides tools to distinguish between string literals and objects. Here are some approaches:

Function check(str)

instanceof check:

if (str instanceof String)
  • Checks if str is an instance of the String constructor.
  • This will return true for objects created using new String().

typeof check:

if (typeof str === "string")
  • Checks if str is of type "string", which applies to string literals.
  • It does not return "string" for new String() because new String() is an object.

Fallback:

return "another type";
  • Handles cases where str is neither a string literal nor a String object (e.g., numbers, arrays, etc.).

Example

In the below example, we are going to define the string in a literal form and an object form. Once the form is defined, we will be using the typeof or instanceof method for checking whether the string is of literal type or object type.

# index.html

<!DOCTYPE html>
<html lang="en">
           <head>
           <title>Checking String type</title>
           <meta charset="UTF-8">
                         <meta http-equiv="X-UA-Compatible"content="IE=edge">
                                 <meta name="viewport" content="width=device-width, initial-scale=1.0">
                                         </head>
                                         <body>
                                         <h1 style="color: green;">Welcome to Tutorials Point</h1>
                                                 <script>
function check(str) {
	if(str instanceof String) {
		return "It is an object of string";
	} else {
		if(typeof str === "string") {
			return "It is a string literal";
		} else {
			return "another type";
		}
	}
}
// Pass a literal
console.log(check("Simply Learning"));

// Pass an object of string
let s = new String("Hi");
console.log(check(s));
</script>
</body>
</html>

Output

 typeof vs instanceof:

Feature typeof instanceof
Purpose Checks the type of a value
Checks if an object is an instance of a class/constructor
String Literal "string"
false
String Object "object" true
Updated on: 2024-12-09T21:56:22+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements