

Javascript: Check if an Object is String – thisPointer
source link: https://thispointer.com/javascript-check-if-an-object-is-string/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

While working in javascript, we sometimes encounter a requirement to check if an object is a string. This article demonstrates easy ways to check if a javascript object is a string using different methods and example illustrations.
Table of Contents:
Check if an Object is a String using typeof and instanceof
Javascript’s typeof returns a string representing the type of the object or the primitive.
Syntax:-
typeof operand typeof(operand)
Javascript’s instanceof returns a boolean value after testing if a particular object is of a specific type, that is, if the prototype property of a constructor appears anywhere in the prototype chain of an object.
Advertisements
Syntax:-
object instanceof constructor
Here, the object is the object to test, and the constructor is the function against which the test gets executed.
Example:-
Check if the objects person1 and person2 are Strings or not.
- person1 = “Veronica”
- person2 = { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’}
Code:-
function checkIfString(_object) { if (typeof _object === 'string' || _object instanceof String) { console.log("It's a String"); } else { console.log("It's not a String"); } } //usage of function checkIfString let person1 = "Veronica"; let person2 = { personFirstName : 'George', personLastName : 'Smith', dateOfBirth : 'Nov 14 1984' , city : 'Santiago' }; checkIfString(person1); checkIfString(person2);
Output:-
It's a String It's not a String
Explanation:-
Here we have created a custom function checkIfString() to check if the object passed as the parameter(_object) is the string. The object clears one of the two checks within the function to be qualified as a String. Either the result of typeof _object should be a ‘string’ or the result of instanceof _object should be a String.
Check if an Object is a String using Object.prototype.toString.call()
Javascript’s toString() method returns the string representation of the object.
Syntax:-
toString()
Example:-
Check if the objects person1 and person2 are Strings or not.
- person1 = “Veronica”
- person2 = { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’}
Code:-
function checkIfString(_object) { return (Object.prototype.toString.call(_object) === '[object String]'); } //usage of function checkIfString console.log(checkIfString(person1)?"It's a String": "It's not a String"); console.log(checkIfString(person2)?"It's a String": "It's not a String");
Output:-
It's a String It's not a String
Explanation:-
Here we have created a custom function checkIfString() to check if the object passed as the parameter(_object) is the string. Within the function, the object is passed to Object.prototype.toString.call(), which is similar to typeof as it determines the type of a javascript object. If the output of this expression returns ‘[object String]’, then the object is qualified to be a String.
Check if an Object is a String using Object.prototype.constructor
Javascript’s Object.prototype.constructor is a property that will return the reference to the Object constructor function that created the object instance.
Example:-
Check if the objects person1 and person2 are Strings or not.
- person1 = “Veronica”
- person2 = { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’}
Code:-
function checkIfString(_object) { if(_object.constructor === String) { console.log("It's a String"); } else { console.log("It's not a String"); } } //usage of function checkIfString() checkIfString(person1); checkIfString(person2);
Output:-
checkIfString(person1); checkIfString(person2);
Explanation:-
Here we have created a custom function checkIfString() to check if the object passed as the parameter(_object) is the string. Within the function, a check is made to check if the constructor that created the object’s instance is of type String or not.
I hope this article helped you check if a particular object is a String in javascript. Good Luck !!!
Advertisements
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK