Javascript: Check if an Object is String

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.

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 !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top