Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the typeof(jQuery)

Tags:

jquery

i just tried this code

console.log(typeof(jQuery))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>

It alerts function, which means the typeof jQuery is function.

My question is, what's the exact type of jQuery? If it's function, how come it has properties like jQuery.browser and jQuery.ajax?

like image 654
Rusi Nova Avatar asked Aug 03 '11 05:08

Rusi Nova


People also ask

What is the use of typeof ()?

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 is the typeof operator?

The typeof operator is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand.

Is typeof a data type?

The Data Type of typeofIt is an operator. Operators ( + - * / ) do not have any data type. But, the typeof operator always returns a string (containing the type of the operand).

What is typeof string?

The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. The operator returns the data type.


1 Answers

The typeof operator when applied to the jQuery object returns the string "function". Basically that does mean that jQuery is a function.

But the typing sort of stops there. Unlike statically typed languages, the number, order, modes, and types of parameters are not taken into account when computing the type of a a function. In JavaScript, it is just a "function."

When you create a function in JavaScript, the function object you create is given two properties, length and prototype, and its prototype is set to Function.prototype so it has inherited properties like apply and call.

And as others have already answered, feel free to add your own properties. a function is just an object.

But be careful about "type." Techncially there are only SIX types in JavaScript: Null, Undefined, Boolean, Number, String, and Object. So the real answer to your question, what is the exact type of jQuery is .... actually ... drumroll .... Object.

Edit for 2021

There are now EIGHT types in JavaScript. Symbol and BigInt have been added since this answer was written a decade ago.

like image 154
Ray Toal Avatar answered Sep 18 '22 18:09

Ray Toal