Tech Study

JavaScript typeof Operator | Typeof Explained

JavaScript typeof Operator

The typeof javascript operator returns a string that indicates the type of the operand’s value.

SYNTAX – typeof operand

Operand – An expression which is representing the object or primitive data whose type is to be returned.

typeof "TechStudy"                 // "string"
typeof 45                   // "number"

typeof NaN                    // "number"

typeof [1,2,3,4]              // "object"

typeof null                   // "object"

The type of method can return one of these primitive data types ( A primitive data value can  be used to refer to a single simple data value with no extra or additional properties and methods.)

  1. Number
  2. Boolean
  3. String
  4. Undefined

It can also be used to return complex data types like functions or objects. It returns “object” for objects, arrays, and null and does not return “object” for functions.

NOTE – The typeOf() operator returns objects for arrays because, in JavaScript, arrays are treated as objects.

Data Type of typeOf() ?

typeOf() is an operator in JavaScript, data types are defined for variables, but not for operators like ( +,-,*,/)

Why NULL is considered as objects?

In the first implementation of JavaScript, its values were represented as a type tag and a value. 0 was the type tag for objects and null was represented as the NULL pointer. And hence, null had 0 as the type tag, hence the typeof return value “object”.

For Example 

console.log(typeof “TechStudy”);

// Expected output: "string"

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more