Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does typeof function return "function"?

If both array and function are object subtypes, then why does typeof function return "function" and typeof array return "object"?

like image 888
Larpee Avatar asked Feb 26 '17 10:02

Larpee


People also ask

What does the typeof of function returns?

The operator returns the data type. There are six possible values that typeof returns: object, boolean, function, number, string, and undefined. The following table summarizes possible values returned by the typeof operator.

Why typeof function is function in JavaScript?

The TypeOf function is an important tool when dealing with complex code. It allows a programmer to quickly check a variable's data type—or whether it's “undefined” or “null”—without going through the code line by line! Additionally, the TypeOf function can also check whether an operand is an object or not.

What does typeof return in JavaScript?

In JavaScript, the typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable.

Why typeof string is function?

This is because String is actually a global constructor. It is used to create strings! The "String" is a string as javascript converts it into a string primitive.

What is the return type of typeof?

The typeof operator can return one of these primitive types: The typeof operator returns "object" for objects, arrays, and null. The typeof operator does not return "object" for functions. The typeof operator returns " object " for arrays because in JavaScript arrays are objects.

Should functions always return only one type?

So the rule that "functions should always return only one type" is rather meaningless. Having said that, there is a beautiful simplicity to functions that return objects which all share the same properties.

What is the difference between +-*/and TypeOf?

It is an operator. Operators ( + - * / ) do not have any data type. But, the typeof operator always returns a string (containing the type of the operand). The constructor property returns the constructor function for all JavaScript variables. You can check the constructor property to find out if an object is an Array (contains the word "Array"):

What is the use of type function in Python?

Python | type() function. type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.


2 Answers

Because the specification defines different behaviour for typeof when it interacts with objects implementing [[Call]] (i.e. functions).

like image 192
Quentin Avatar answered Oct 20 '22 05:10

Quentin


Very ugly, yet correct answer: because ECMAScript specification says so. Keep in mind that on the low level, every array is basically a mere object with "length" property.

If you want to check if given object is actually an array, use Array.isArray.

like image 4
Hubert Zub Avatar answered Oct 20 '22 05:10

Hubert Zub