JavaScript Checks if The Variable is a String

JavaScript Checks if The Variable is a String | A string represents a group of characters in JavaScript that can be enclosed in single or double quotes. The JavaScript variable contains strings and numbers to execute any operation in many scenarios.

Therefore, before carrying out any process, checking the variable type in JavaScript is crucial to determine the correct data type. The typeof and instanceof operators in JavaScript are used to determine whether a variable is a string. The many ways to determine whether a variable is a string are explained in this article.

The following is a list of the learning objectives for this blog:-

  1. Using the typeof Operator to Verify a String Variable
  2. Using the instanceof Operator to Verify a String Variable

How Does JavaScript Checks if The Variable Is a String?

JavaScript can check a variable’s type by producing a Boolean result. In JavaScript, the typeof and instanceof operators are used to determine whether a variable’s type is a string. The typeof operator returns the variable’s datatype, which can be used to determine whether the variable is a string. Similarly, in JavaScript, the instanceof operator determines the type of the variable at runtime.

Let’s practice the specifics of how JavaScript’s typeof operator works.

Using the typeof Operator to Verify a String Variable

The data type of passing operands is determined by a method, which is a modified typeof operator. The operator then determines in JavaScript if the variable is a string. As an illustration, the syntax is provided below.

Syntax:-
typeof var; 

The variable “var” is evaluated in this syntax to determine its data type and whether it is a string.

// Example to check if the variable is string
var var1 = 'This is Know Program';
console.log(typeof var1);

Output:-

string

Using the instanceof Operator to Check a String Variable

In JavaScript, the type of an instance is determined using the instanceof operator. An example is considered to determine whether a variable is a string. The syntax and code, for example, are shown below.

Syntax:-
Var instanceof String;

The instanceof operator is used in this syntax to determine whether the variable is an instance of the String data type.

var str = new String("Welcome to Know Program");
if (str instanceof String) {
    console.log('Variable is string');
}
else {
    console.log('Variable is not string');
}

Output:-

Variable is string

Ending Notes:-

The typeof and instanceof operators in JavaScript can be used on variables that are strings. While the instanceof operator determines whether a particular variable is a member of the data type string, the typeof operator yields the variable’s datatype. They produce accurate or erroneous results. Here, two approaches are described in detail with suitable JavaScript examples for determining whether a variable is a string.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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