Check If A Variable Is A String In JavaScript

You can check if a variable is a String in JavaScript by using any of these methods –

  1. typeof operator
  2. Object.prototype.toString.call() Method
  3. Lodash library
  4. jQuery

Let’s have a look at each of these methods in detail below.

check if a variable is a string in javascript

typeof Operator

The easiest and most common way to check if a variable is a string in JavaScript is by using the typeof operator. The typeof operator is an operator that returns a string representing the data type of its operand. So, all you need to do is pass in the variable whose data type you want to check as the operand to the typeof operator and it will return a string with the name of the data type.

If the variable is a string, it will return “string”.

Here’s an example –

var myVar = "Hello";

console.log(typeof myVar); // outputs "string"

As you can see from the code above, we have a variable myVar that contains a string “Hello”. We then use the typeof operator to check the data type of this variable. As expected, it returns “string”.

So, to check if a variable is a String, you can use an if statement as follows –

var myVar = "Hello";

if(typeof myVar === "string") {

console.log("myVar is a string");

} else {

console.log("myVar is not a string");

}

As you can see, we are using the === operator to check if the data type of myVar is exactly equal to “string”.

Object.prototype.toString.call() Method

Another way to check if a variable is a string in JavaScript is by using the Object.prototype.toString method. The Object.prototype.toString method is a method that returns a string representing the object on which it is called. So, all you need to do is pass in the variable whose data type you want to check as the argument to this method and it will return a string with the name of the data type.

If the variable is a string, it will return “[object String]”.

Here’s an example –

var myVar = "Hello";

console.log(Object.prototype.toString.call(myVar)); // outputs "[object String]"

As you can see from the code above, we have a variable myVar that contains a string “Hello”. We then use the Object.prototype.toString method to check the data type of this variable. As expected, it returns “[object String]”.

So, to check if a variable is a String, you can use an if statement as follows –

var myVar = "Hello";

if(Object.prototype.toString.call(myVar) === "[object String]") {

console.log("myVar is a string");

} else {

console.log("myVar is not a string");

}

As you can see, we are using the === operator to check if the return value of Object.prototype.toString method is exactly equal to “[object String]”.

Lodash Library

If you are already using the Lodash library in your project, then you can use the _.isString method to check if a variable is a string in JavaScript.

The _.isString method is a Lodash utility function that returns true if the value passed to it is a string, false otherwise. So, all you need to do is pass in the variable whose data type you want to check as the argument to this method and it will return either true or false.

Here’s an example –

var myVar = "Hello";

console.log(_.isString(myVar)); // outputs true

As you can see from the code above, we have a variable myVar that contains a string “Hello”. We then use the _.isString method to check the data type of this variable. As expected, it returns true.

So, to check if a variable is a String, you can use an if statement as follows –

var myVar = "Hello";

if(_.isString(myVar)) {

console.log("myVar is a string");

} else {

console.log("myVar is not a string");

}

As you can see, we are using the if statement to check if the _.isString method returns true or false. If it returns true, that means myVar is a string, if it returns false, that means myVar is not a string.

jQuery.type() Method

If you are already using the jQuery library in your project, then you can use the jQuery.type method to check if a variable is a string in JavaScript.

The jQuery.type method is a jQuery utility function that returns the type of the value passed to it as an argument. So, all you need to do is pass in the variable whose data type you want to check as the argument to this method and it will return a string with the name of the data type.

If the variable is a string, it will return “string”.

Here’s an example –

var myVar = "Hello";

console.log($.type(myVar)); // outputs "string"

As you can see from the code above, we have a variable myVar that contains a string “Hello”. We then use the jQuery.type method to check the data type of this variable. As expected, it returns “string”.

So, to check if a variable is a String, you can use an if statement as follows –

var myVar = "Hello";

if($.type(myVar) === "string") {

console.log("myVar is a string");

} else {

console.log("myVar is not a string");

}

As you can see, we are using the === operator to check if the return value of jQuery.type method is exactly equal to “string”.

Conclusion

In this article, we looked at how to check if a variable is a string in JavaScript. We saw how to use the typeof operator, the Object.prototype.toString method, the Lodash _.isString method and the jQuery.type method to achieve this.

I hope you found this article helpful. Thank you for reading.

Leave a Reply