Check if a variable is a string using JavaScript
Checking if a variable is a string in JavaScript is a common task to ensure that the data type of a variable is what you expect. This is particularly important when handling user inputs or working with dynamic data, where type validation helps prevent errors and ensures reliable code execution.
Below are the following methods through which we can check the type of variable is a string:
Table of Content
Using typeOf Operator
In this approach, we are using typeOf operator which tells us the type of the given value. we are getting the type of the value by the use of the typeOf operator and we are using if-else for returning our ans.
Example: This example shows the implementation of the above-explained approach.
let boolValue = true;
let numValue = 17;
let bool, num;
if (typeof boolValue == "string") {
bool = "is a string";
} else {
bool = "is not a string";
}
if (typeof numValue == "string") {
num = "is a string";
} else {
num = "is not a string";
}
console.log(bool);
console.log(num);
Output
is not a string is not a string
Using Instanceof Operator
The instanceof operator in JavaScript is used to check the type of an object at run time.
Syntax:
let gfg = objectName instanceof objectType
Example: This example shows the implementation of above-explained appraoch.
let variable = new String();
if (variable instanceof String) {
console.log("variable is a string.");
} else {
console.log("variable is not a string.");
}
Output
variable is a string.
Underscore.js _.isString()
The _.isString() function is used to check whether the given object element is string or not.
Syntax:
_.isString( object );
Example: This example shows the implementation of above-explained appraoch.
let _ = required("underscore");
let info = {
Company: 'GeeksforGeeks',
Address: 'Noida',
Contact: '+91 9876543210'
};
console.log(_.isString(info));
let str = 'GeeksforGeeks';
console.log(_.isString(str));
Output:
false
true
Using Lodash _.isString() Method
In this approach, we are using the Lodah _.isString() method that returns boolean response for the given value.
Example: This example shows the implementation of above-explained appraoch.
// Requiring the lodash library
const _ = require("lodash");
// Use of _.isString() method
console.log(_.isString('GeeksforGeeks'));
console.log(_.isString(true));
Output:
true
false
Using Object.prototype.toString.call Method
The Object.prototype.toString.call method is a reliable way to determine the type of a variable. It returns a string indicating the type of the object.
Syntax:
Object.prototype.toString.call(variable) === '[object String]'
Example: This example shows the implementation of the above-explained approach.
function checkIfString(variable) {
if (Object.prototype.toString.call(variable) === '[object String]') {
console.log("variable is a string.");
} else {
console.log("variable is not a string.");
}
}
// Test cases
checkIfString("Hello, World!");
checkIfString(42);
checkIfString(true);
checkIfString({});
checkIfString(new String("Test"));
Output
variable is a string. variable is not a string. variable is not a string. variable is not a string. variable is a string.