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.

// 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';

#Javascript typeof NaN

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.

// app.js

console.log(typeof NaN);

See the following output.

➜  es git:(master) ✗ node app
number
➜  es git:(master) ✗

However, it is the numeric data type whose value cannot be represented using the actual numbers.

This also explains why not all NaN values are equal. See the below code.

// app.js

const x = NaN;
const y = NaN;

console.log(x === y);

See the following output.

➜  es git:(master) ✗ node app
false
➜  es git:(master) ✗

#The typeof an array

The typeof an array is an object. In JavaScript, arrays are technically objects; just with special behaviors and abilities.

// app.js

let st3 = ['Eleven', 'Dustin', 'Lucas'];
console.log(typeof st3);

See the following output.

➜  es git:(master) ✗ es git:(master) ✗ node app
object
➜  es git:(master) ✗

We can differentiate an Array object from an Object object using the Array.isArray() method.

#Javascript typeof class

See the following code.

// app.js

console.log(typeof class Foo { });

See the output.

➜  es git:(master) ✗ node app
function
➜  es git:(master) ✗

Finally, we have Classes. The Classes were introduced in ES2015 (ES6) as a better syntax for the prototype-based inheritance. Before Classes, to make the inheritable object, we would have to use a 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.