Reputation: 753
I had a chance to met this question but unable to find an answer.
var arr = [];
Why does typeof Array
returns "function"
and typeof arr
returns an "Object"
?
Can anyone explain please.
Upvotes: 0
Views: 91
Reputation: 38094
When you write typeof Array
, it means that you are getting type of constructor function. As class
is under the hood is constructor function. Let me show an example:
class Person {
constructor(firstName, lastName, address) {
this.firstName= firstName;
this.lastName = lastName;
this.address= address;
}
getFullName () {
return this.firstName + " " + this.lastName ;
}
}
and to create an instance of the class:
let car = new Person ("Jon", "Freeman", "New York");
In the above code, we've created a variable car
which references to function construtor defined in the class:
function Person (firstName, lastName, address) {
this.firstName = firstName,
this.lastName = lastName,
this.address = address,
this.getFullName = function () {
return this.firstName+ " " + this.lastName;
}
}
So this is a reason why typeof Array
returns function
.
Upvotes: 1