How to test a string as a literal and as an object in JavaScript ?
Last Updated :
21 Jul, 2023
In this article, we learn how to test a string as a literal and as an object using JavaScript.
What is JavaScript Literal?
Literals are ways of representing fixed values in source code. In most programming languages, values are notated by integers, floating-point numbers, strings, and usually by booleans and characters, enumerated types and compound values, such as arrays, records, and objects, are notated by other names as well.
Each object consists of an unordered list <ol> of primitive data types (and sometimes reference data types) stored as pairs of names and values. In a list, each item is a property.
There are several methods that can be used to test a string as a literal and as an object.
We will explore all the above methods along with their basic implementation with the help of examples.
The typeof operator in JavaScript returns a string that identifies the data type of an expression. It is used to determine the data type (returns a string) of its operands. Operands can either be literals or data structures, such as variables, functions, or objects. An operator returns the type of data. The result of typeof can be an object, a boolean, a function, a number, a string, or an undefined value.
Example: In this example, we are going to check if the string is an object or a literal using the if-else condition.
JavaScript
const myString = "Hello, Geeks";
console.log(typeof myString);
const myStringObj = new String("Hello, GeeksforGeeks");
console.log(typeof myStringObj);
instanceof operator: It checks whether the LHS (left-hand side) object is an object of the RHS (right-hand side) class or not. If the object is of that particular class, then it returns true otherwise false.
Example: The first myString is not an instance of String, as it's a literal. The second myStringObj is an instance of String as it's an object.
JavaScript
//checking for string
const myString = "Hello, Geeks";
console.log(myString instanceof String);
// checking for mystringObj is string or not
const myStringObj = new String("Hello, GeeksforGeeks");
console.log(myStringObj instanceof String);
Approach 3: Using constructor property
Using the constructor property to check if a variable is a string literal or an object of the String constructor, the constructor property of the input matches String, it is considered a string literal. If the constructor property matches String.prototype.constructor, it is considered an object of the String constructor.
Example: In this example, the check() function uses the constructor property to determine whether the input is a string literal or an object of the String constructor
JavaScript
function check(str) {
if (str.constructor === String) {
return "It is a string literal";
} else if (str.constructor === String.prototype.constructor) {
return "It is an object of string";
} else {
return "Another type";
}
}
// Pass a string literal
console.log(check("Hello, Geeks"));
// Pass an object of string
let result = new String("Hello, GeeksforGeeks");
console.log(check(result));
OutputIt is a string literal
It is a string literal
Similar Reads
How to change JSON String into an Object in JavaScript ?
In this article we are going to learn how to change JSON String into an object in javascript, JSON stands for JavaScript object notation. It is the plain format used frequently as a communication medium on the internet. It appears close to OOP language like JavaScript but cannot be accessed like Jav
3 min read
JavaScript - How to Find the First and Last Character of a String?
Here are the various methods to find the first and last character of a string in JavaScript.1. Using charAt() MethodThe charAt() method retrieves the character at a specified index.JavaScriptconst s = "JavaScript"; const first = s.charAt(0); const last = s.charAt(s.length - 1); console.log(first); c
2 min read
How to print the content of an object in JavaScript ?
To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: JavaScript // Given Object const obj = { name: 'John', age: 30, city:
3 min read
How to convert an object to string using JavaScript ?
To convert an object to string using JavaScript we can use the available methods like string constructor, concatenation operator etc. Let's first create a JavaScript object. JavaScript // Input object let obj_to_str = { name: "GeeksForGeeks", city: "Noida", contact: 2488 }; Examp
4 min read
How to Check a Key Exists in JavaScript Object?
Here are different ways to check a key exists in an object in JavaScript.Note: Objects in JavaScript are non-primitive data types that hold an unordered collection of key-value pairs. check a key exists in JavaScript object1. Using in Operator The in operator in JavaScript checks if a key exists in
2 min read
How to check a JavaScript Object is a DOM Object ?
Checking if a JavaScript object is a DOM object involves verifying whether the object represents an element or component within the Document Object Model (DOM) of an HTML or XML document. This can be done by checking if the object is an instance of Node, Element, or other specific DOM interfaces.Wha
2 min read
How to get dynamic access to an object property in JavaScript ?
In JavaScript, an object is a collection of properties, where each property consists of a key-value pair. Objects are a fundamental data type in JavaScript. You can create an object in JavaScript in the following ways: By using the object literal notation, which is a comma-separated list of key-valu
7 min read
How to check a given string is an anagram of another string in JavaScript ?
In this article, we will learn how to check a given string is an anagram of another string in JavaScript. Before that, we should learn what is an anagram.An anagram is a word or sentence, which usually contains all the original letters exactly once, in order to arrange the letters of a different ter
3 min read
How to check whether an object exists in javascript ?
Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to
3 min read
How to Check if an Object has a Specific Property in JavaScript ?
In JavaScript, objects can have properties that store data or functions. Sometimes it is necessary to check whether an object has a specific property. This can be useful, for example, when you want to avoid calling a function on an object that doesn't have that function defined as a property. In thi
3 min read