💥 What is JavaScript typeof💥

💥 What is JavaScript typeof💥

JavaScript Typeof: A Simple Guide To Learning Typeof Operator in JS

Checking data types for variables is vital in any programming language to ensure a smooth and error-free development process🤫. However, this becomes even more essential for accuracy when it comes to dynamically typed languages such as Javascript.

In Javascript, one variable can hold multiple value types within the same file as shown below:👇

let x = 1;
x = 'blue';
x = true;
x = Boolean(true);
x = String('Be positive');

In this write-up, we will learn all about the typeof operator, what it is and how to use it; but first, we need to understand what types and data types are🤔.

So let’s get started…

What are Types?🤔

The type of a variable determines what it can or can’t do.🤨

It determines what value the variable may take and what can be done with that value.

For example, a number-type variable containing a numeric value can be used in arithmetic. In contrast, a string-type variable containing a sequence of words cannot be used in arithmetic calculations, but it can be joined to other strings to create longer strings.

What is Data Types in JavaScript?🤔

It's vital to take a quick look at the JavaScript data types before discussing type-checking using typeof. Although the JavaScript data types are not covered in length in this article, you can learn a few things as you go.🤗

  • Number: As the name implies, it consists of numbers

  • Boolean: Booleans can have only two different values; true or false.

  • String: Strings are a collection of alphanumeric characters.

  • Undefined: Empty or undeclared variable.

  • null (the value null)

  • Symbol

  • Object: Bundle/Collection of Data

Primitive types refer to the initial six data types. Other than the first six data types, all other data types are objects and can be referred to as reference types. A collection of properties in the form of name and value pairs forms an object type.

The typeof is not a function, but rather it is an operator. In programming languages, functions and operators are different; they may behave similarly but are synthetically and semantically different.

What is typeof🥸

Javascript typeof is an inbuilt operator that returns the string indicating a type of the unevaluated operand. JavaScript typeof operator returns a data type of its operand in the form of the string.

The operand can be any object, function or variable. You can use a JavaScript typeof operator to find the type of the JavaScript variable.

The typeof operator returns the type of the variable or an expression.

How To Use typeof in Javascript👇

The typeof operator does just that – it tells you the type of a given variable. Here’s the syntax:

typeof operand

Variable Typetypeof Output
Undefined“undefined”
Null“object”
Boolean“boolean”
Number“number”
BigInt“bigint”
String“string”
Symbol“symbol”
Function object“function”
Any other object“object”

Below👇, you can see what value the typeof operator returns based on various values passed to it:

typeof 32; // 'number'

typeof 1.89; // 'number'

typeof NaN; // 'number' - this variable should be numeric, but a numeric value was not able to be assigned to it, so it is a number variable with a NaN (Not a Number) value to indicate as such

typeof ''; // 'string'

typeof 'foo'; // 'string'

typeof true; // 'boolean'

typeof false; // 'boolean'

typeof undefined; // 'undefined'

typeof {property: 'value'}; // 'object'

Examples😉

Now we will look at a few examples of typeof operators. The typeof operator is present in many programming languages, but we will use the JavaScript syntax for these examples😌.

In the code given below,

  • we have declared a variable named age and assigned it a value of 10. Then we used the typeof operator to determine the datatype of age. The typeof operator was put inside console.log() to output the return value of the typeof operator on the console.
var age = 10;
console.log(typeof(age));

  • If we assign a new value to age, i.e., ten instead of 10, then the output changes to:
var age = 'ten';
console.log(typeof(age));

  • Now, if we do not assign any value to age and use the typeof operator on it then the operator will return the following value:
var age;
console.log(typeof(age));

  • We can use the same method to determine other data types as well like boolean, object and symbol, etc.:
var x = true;
console.log(typeof(x));

As you have already seen in the above examples🤫, the typeof operator can be combined with other functions and methods such as conolse.log. It can also be combined with conditionals, loops, etc.

  • In the example given below, the typeof operator is used with conditional statements:
var age = 5;
if (typeof(age) == 'number')
{
console.log('The provided number is in the form of digits.')  
}
else
{
console.log('The provided number is not in the form of digits.')
}

Now, if we change the age from 5 to five, then the output changes to:

Conclusion🤠

The typeof operator returns the data type of the operand (the variable which is passed as the parameter to the operator)😯.

It is very helpful in programming languages such as JavaScript as it has dynamic data types. Dynamic data type means that the same variable can store different data types within a single program.

That's it for the day see you😍 in the next blog until then....

keep reading my friend🤗

Did you find this article valuable?

Support Chhakuli Zingare by becoming a sponsor. Any amount is appreciated!