Navigate back to the homepage

How to check type of variable in javascript

Infinitbility
JavaScript
September 6th, 2021 · 1 min read
How to check type of variable in javascript

Hello Friends 👋,

Welcome To Infinitbility! ❤️

JavaScript provides typeof keyword to check the datatype of any variable, it will return string, number, boolean, symbol, undefined, object, and function.

Due to the javascript codebase, it will work also in React, React Native, typescript, node, deno, next, vue, gatsby, and all javascript libraries.

Today, we are going to use typeof in every possible example. let’s start.

typeof string example

typeof return string if we check any data available in quotes. like below typeof string example.

1let data = "Infinitbility";
2console.log(typeof data);
3
4// Output
5// string

typeof number example

typeof will return number if we check any variable store 0 to 9 numbers without quotes.

The type of NaN, which stands for Not a Number is, surprisingly, a number. The reason for this is, in computing, NaN is actually technically a numeric data type.

1let data = 0123;
2console.log(typeof data);
3console.log(typeof NaN);
4
5// Output
6// number

typeof boolean example

When we check any conditional statement or true or false value then typeof return boolean.

1let data = "Infinitbility";
2console.log(typeof data === "string");
3
4console.log(typeof true);
5
6// Output
7// boolean

typeof symbol example

A symbol is a built-in object whose constructor returns a symbol primitive — also called a Symbol value or just a Symbol — that’s guaranteed to be unique.

1typeof Symbol();
2typeof Symbol('foo');
3typeof Symbol.iterator;
4
5// Output
6// symbol

typeof undefined example

typeof will return undefined when variables haven’t defined value.

1typeof undefined;
2
3let data;
4typeof data;
5
6
7// Output
8// undefined

typeof objects example

typeof will return object for both object and array ( The typeof an array is an object. In JavaScript, arrays are technically objects; just with special behaviors and abilities ).

1typeof {name: 'krunal'};
2typeof [21, 19, 46];
3
4// Output
5// object

typeof function example

typeof return function for functions, and class.

1typeof function() {};
2typeof class C {};
3typeof Math.pow;
4
5// Output
6// function

Thanks for reading…

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]

Tutorials

React Native

Categories

More articles from Infinitbility

How to check if device is android or ios in react native

How to check if device is android or ios in react native

React Native check device is android or ios example

September 5th, 2021 · 1 min read
How to check connected devices in react native

How to check connected devices in react native

React Native check connected devices list example

September 4th, 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]