Skip to content

Commit 40f917d

Browse files
arthurvrKrinkle
authored andcommitted
using-jquery-core/utility-methods: add type checking section
Fixes gh-404 Closes gh-597
1 parent e265be4 commit 40f917d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

page/javascript-101/testing-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ if ( Object.prototype.toString.call( myArray ) === "[object Array]" ) {
4444
}
4545
```
4646

47-
jQuery also offers utility methods to help determine the type of an arbitrary value.
47+
jQuery also offers [utility methods](/using-jquery-core/utility-methods#testing-type) to help determine the type of an arbitrary value.

page/using-jquery-core/utility-methods.md

+33
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,36 @@ var myObject = {
103103
$( "#foo" ).click( myObject.myFn ); // HTMLElement #foo
104104
$( "#foo" ).click( $.proxy( myObject, "myFn" ) ); // myObject
105105
```
106+
107+
## Testing Type
108+
109+
As seen in [the JavaScript 101 section](http://learn.jquery.com/javascript-101/testing-type/) the `typeof` operator can be inconsistent or confusing. So instead of using `typeof`, jQuery offers utility methods to help determine the type of a value.
110+
111+
First of all, you have methods to test if a specific value is of a specific type.
112+
113+
```
114+
$.isArray([]); // true
115+
$.isFunction(function() {}); // true
116+
$.isNumeric(3.14); // true
117+
```
118+
119+
Additionally, there is `$.type()` which checks for the internal class used to create a value. You can see the method as a better alternative for the `typeof` operator.
120+
121+
```
122+
$.type( true ); // "boolean"
123+
$.type( 3 ); // "number"
124+
$.type( "test" ); // "string"
125+
$.type( function() {} ); // "function"
126+
127+
$.type( new Boolean() ); // "boolean"
128+
$.type( new Number(3) ); // "number"
129+
$.type( new String('test') ); // "string"
130+
$.type( new Function() ); // "function"
131+
132+
$.type( [] ); // "array"
133+
$.type( null ); // "null"
134+
$.type( /test/ ); // "regexp"
135+
$.type( new Date() ); // "date"
136+
```
137+
138+
As always, you can check the [API docs](http://api.jquery.com/jQuery.type/) for a more in-depth explanation.

0 commit comments

Comments
 (0)