AppDividend
Latest Code Tutorials

Javascript Typeof Example | typeof Operator Tutorial

0

Javascript Typeof Example | typeof Operator Tutorial is today’s topic. The typeof operator returns the string indicating a type of the unevaluated operand. In JavaScript, the 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.

Javascript Typeof Example

Let’s see the syntax of the typeof operator.

typeof operand

The operand parameter is an expression representing the object or primitive whose type is to be returned. See the following example.

// app.js

console.log(typeof 'appdividend');

The output is following.

 

Javascript Typeof Example | typeof Operator Tutorial

We can also check the Number as well.

// app.js

let appInt = 21;
console.log(typeof (appInt));

It will return the number.

Next. Let’s check the object.

// app.js

const obj = {
  name: 'krunal',
  age: 26
};
console.log(typeof (obj));

 

typeof object in javascript

Use typeof as boolean

We can compare the variable’s datatype using the typeof operator, and we get the result in terms of true or false. See the following example.

// app.js

console.log(typeof 'appdividend' === 'string');

See the following output.

Use typeof as boolean

 

We can do it the same for the number and other data types as well.

// app.js

console.log(typeof 21 === 'number');

We get true; if the data type does not match then, it will return false.

typeof Undefined

In JavaScript, the variable without any value has the value undefined. The type is also undefined.

Let’s see the following example.

// app.js

let jet;
console.log(typeof (jet));

See the following output.

 

typeof Undefined

typeof null

The null is an exception to javascript since the beginning. See the following example.

Related Posts
1 of 49
// app.js

console.log(typeof null === 'object');

The output is true. That means, javascript counts null as an object.

Since starting, JavaScript values were represented as the type tag and a value. The type tag for objects was 0. The null was described as the NULL pointer (0x00 in most platforms). Consequently, the null had 0 as type tag, hence the “object” typeof return value.

More Example of a typeof operator

Let’s see the typeof operator on different Javascript datatypes.

// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(1) === 'boolean';
typeof !!(1) === 'boolean';


// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'


// Undefined
typeof undefined === 'undefined';

// Objects
typeof {name: 'krunal'} === 'object';

// use Array.isArray or Object.prototype.toString.call
typeof [21, 19, 46] === 'object';

// Functions
typeof function() {} === 'function';
typeof class C {} === 'function';
typeof Math.pow === 'function';

Finally, Javascript Typeof Example | typeof Operator Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.