Type checking in JavaScript ( typeof and instanceof)

Type checking in JavaScript ( typeof and instanceof)

JavaScript is an inexactly composed language, so there is no limitation on the variable's type.

For example, on the off chance that you've made a variable with a string type, later you can assign to a similar variable a number:

let msg = 'Hello World'; 

msg = 10;

Such dynamism gives you flexibility and simplifies variables declaration.

On the opposite side, you can never be certain that a variable contains a worth of a particular type. For instance, the accompanying function say(msg) anticipates a string contention, nonetheless, you can summon the function with a argument:

function say(msg) {
  return `Hello, ${msg}!`
}

say('Hello World'); 

say(true);    
say([1]);

That is the reason, now and again, you need to check the variable's type in JavaScript — utilizing typeof operator, just as instanceof to check instance types.

Let’s see in more detail how to use typeof and instanceof operators in JavaScript.

1. typeof operator

In JavaScript, you can discover primitive types like strings, numbers, booleans. Also, there are function, objects, and the unique special value undefined and null.

typeof is the operator that we should you decide the type of the expression:

const typeAsString = typeof expression;

where expression assesses to a worth which type you'd prefer to discover. expression can be a variable myVariable,

Let’s see how typeof operator works for various type:

I.Strings

const msg= 'Hello World!';
typeof msg;

II.Number

const number = 7;
typeof number; 
typeof NaN;

III.Booleans:

const check = true;
typeof check;

IV.Symbols:

const symbol = Symbol('key');
typeof symbol;

V.undefined:

const blank = undefined;
typeof blank;

VI.Objects:

const object = { name: 'Shantun' };
typeof object; 

const array = [1, 4, 5];
typeof array; 

const regExp = /Hi/;
typeof regExp;

VII.Functions:

function say(msg) {
  return `Hello, ${msg}!`
}

say('Hello World');

2. instanceof operator

The typical method to utilize a JavaScript function is to invoke it's anything but a couple of parentheses after its name:

function say(msg) {
  return `Hello, ${msg}!`
}

say('Hello World');

say('Hello World'); is a regular function invocation.

Be that as it may, JavaScript function can accomplish more: they can even construct objects! To make a function construct objects, simply utilize new keyword before the regular function invocation:

function say(who) {
  this.msg= `Hello, ${who}!`;
}

const longmsg = new say('Hello World');
longmsg .msg;

new say('Hello World') is a constructor invocation that makes the instance longmsg .

How might you check in JavaScript that a specific instance was made with a specific constructor? :

const bool = object instanceof Constructor;

where object is an expression that assesses to an object, Contructor is a class or function that develops objects. instanceof assesses to a boolean.

longmsg instance was made utilizing say constructor, so longmsg instanceof say assesses to true.

You can construct object by using class, For eg. et’s define a class language and then created an instance of it myStack:

class Language{
  constructor(name) {
    this.name = name;
  }
}

const myStack= new Language('Javascript');

Here new Language('Javascript') is constructor invocation which create an instance myStack.

Now myStack instanceOf Language also evaluates to true.

myStack instanceof Language;

3. Summary

JavaScript is an approximately composed language, implying that there is no restrictions on what type a variable can have.

typeof expression is the operator that allows you to decide the type of expression. typeof assesses to one of the values: string, number, boolean, symbol, undefined, object, function.

Share your valuable feedback so that I can improve my article further.

Thank you for reading

Did you find this article valuable?

Support Shantun Parmar by becoming a sponsor. Any amount is appreciated!