How to Check If Variable is String in JavaScript

Programmers often deal with many data types, like numbers, objects, strings, etc. For example, the string data type is one of the most widely used data types in JavaScript and almost all other programming languages. Today in this article, we will learn how to know if the variable is of string data type.

How to Check If Variable is String in JavaScript

To check if a variable is a string in JavaScript, use the typeof() function. The typeof() is a built-in JavaScript function used to check the data types of the variable. The typeof operator will return an object for strings.

Syntax

typeof varName;

Parameter

Variable name 

Return value

The data type of the variable.

Example 1

const age = "I am 27 years old";

const type = typeof (age);

console.log(type);

Output

string

Explanation

Here, we have first defined a variable named age using the keyword const. Then we have used another variable named type using the const keyword where we have stored the data type of the variable age. typeof() takes the variable name as the parameter. Then we printed the variable value of type in the console, which was a string.

Example 2

const age = "I am 27 years old";

const type = typeof (age);

if (type == "string") {

 console.log("Variable is a string")

}

else {

 console.log("Variable is not a string")

}

Output

Variable is a string

Explanation

In the above code, we have created the variable named age using the const keyword. Then we created another variable named type, where we stored the value of the variable’s data type. Then, we checked the data type of the variable. Since the variable’s data type is a string, the condition is evaluated to true, and therefore the first message is printed in the console.

Using Object.prototype.toString.call() function

The Object.prototype.toString.call() is a built-in JavaScript function that returns a string representing the object. If we use the function, take the variable name as the parameter, and check if this is equal to [object string] and true is returned, then the variable is of string data type.

Parameter

Variable name

Return value

Boolean true or boolean false

Example

function check_if_string(name) {

 return Object.prototype.toString.call(name) === '[object String]';

}

const myname = "Hello my name is John";

if (check_if_string(myname)) {

 console.log("This is a string data type.")

}

else {

 console.log("This is not a string data type")

}

Output

This is a string data type

Explanation

Here we have created a function named check_if_string. It takes the variable name as the parameter and returns the truth value of another built-in function Object.prototype.toString.call().

The variable we created here is string data type; hence, the return value is boolean true. As a result, javascript prints the first message in the console.

Using _.isString() method in JavaScript

The _isStirng method is a lodash library function that is used to check whether the given object element is a string or not.

Parameter

Variable name

Return value

Boolean true or boolean false

Example 1

const _ = require('lodash'); // or underscore

const myname = "Hello my name is John";

if (_.isString(myname)) {

 console.log("This is a string data type")

}

else {

 console.log("This is not a string data type")

}

Output

This is a string data type

Explanation

In the above code, we first imported the library named lodash using the const variable and require function. The require() function takes the library’s name as the parameter.

Then, we created another variable named myname using the keyword const. Finally, we stored a value of string data type there.

Then we used the _.isString() function to check if the given variable is of string data type. Here the given variable name is of string data type, and hence the return value was boolean true.

The program, therefore, prints the first message in the console-This is a string data type.

Example 2

const _ = require('lodash');

const myname = 885;

if (_.isString(myname)) {

 console.log("This is string a data type.");

}

else {

 console.log("This is not a string data type.");

}

Output

This is not a string data type

Explanation

In the above code, the variable named myname is an integer data type. As a result, the second message got printed in the console.

Conclusion

In the above article, we have seen how to check whether a given variable is of string data type. However, programmers often need to check the same for debugging purposes, create some logic in the program, etc.

That’s it for this tutorial.

Related posts

How to Check If String is Empty in JavaScript

How to Check If an Object has a Key in JavaScript

How to Check If Variable Exists in Javascript

Leave a Comment