Navigate back to the homepage

How to check type of variable in typescript?

Infinitbility
TypeScript
October 29th, 2021 · 1 min read
How to check type of variable in typescript?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

Same like javascript, TypeScript have typeof Operator to check datatype of variable. using typeof we are able to check any variable type and compare with our own condition.

Let start today tutorial How to check type of variable in typescript?

TypeScript provide typeguard and typeof one of them. we will all of them but in this tutorial we will focus on typeof.

typeof example

1let str: string = "01234";
2console.log(typeof str);
3
4if(typeof str == 'string'){
5 console.log("str is string");
6}

Output

Typescript, typeof, example

Handle multiple probabilities of data type

1let str: string | number;
2
3str = 123;
4
5console.log(typeof str);
6
7if(typeof str == 'number'){
8 console.log("str is number");
9}

Output

Typescript, typeof, example

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]l.com

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to convert number to string in typescript?

How to convert number to string in typescript?

Example of number to string conversion in typescript

October 28th, 2021 · 1 min read
How to convert string to array in typescript?

How to convert string to array in typescript?

Example of string to array conversion in typescript

October 28th, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]