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.
let data = "Infinitbility";
console.log(typeof data);
// Output
// 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.
let data = 0123;
console.log(typeof data);
console.log(typeof NaN);
// Output
// number
typeof boolean example
When we check any conditional statement or true or false value then typeof return boolean.
let data = "Infinitbility";
console.log(typeof data === "string");
console.log(typeof true);
// Output
// 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.
typeof Symbol();
typeof Symbol('foo');
typeof Symbol.iterator;
// Output
// symbol
typeof undefined example
typeof will return undefined when variables haven’t defined value.
typeof undefined;
let data;
typeof data;
// Output
// 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 ).
typeof {name: 'krunal'};
typeof [21, 19, 46];
// Output
// object
typeof function example
typeof return function for functions, and class.
typeof function() {};
typeof class C {};
typeof Math.pow;
// Output
// function
Thanks for reading…