Posted in: Using jQuery Core

Utility Methods

jQuery offers several utility methods in the $ namespace. These methods are helpful for accomplishing routine programming tasks. For a complete reference on jQuery utility methods, visit the utilities documentation on api.jquery.com.

Below are examples of a few of the utility methods:

link $.trim()

Removes leading and trailing whitespace:

1
2
// Returns "lots of extra whitespace"
$.trim( " lots of extra whitespace " );

link $.each()

Iterates over arrays and objects:

1
2
3
4
5
6
7
$.each([ "foo", "bar", "baz" ], function( idx, val ) {
console.log( "element " + idx + " is " + val );
});
$.each({ foo: "bar", baz: "bim" }, function( k, v ) {
console.log( k + " : " + v );
});

The method .each() can be called on a selection to iterate over the elements contained in the selection. .each(), not $.each(), should be used for iterating over elements in a selection.

link $.inArray()

Returns a value's index in an array, or -1 if the value is not in the array:

1
2
3
4
5
var myArray = [ 1, 2, 3, 5 ];
if ( $.inArray( 4, myArray ) !== -1 ) {
console.log( "found it!" );
}

link $.extend()

Changes the properties of the first object using the properties of subsequent objects:

1
2
3
4
5
6
7
var firstObject = { foo: "bar", a: "b" };
var secondObject = { foo: "baz" };
var newObject = $.extend( firstObject, secondObject );
console.log( firstObject.foo ); // "baz"
console.log( newObject.foo ); // "baz"

If you don't want to change any of the objects you pass to $.extend(), pass an empty object as the first argument:

1
2
3
4
5
6
7
var firstObject = { foo: "bar", a: "b" };
var secondObject = { foo: "baz" };
var newObject = $.extend( {}, firstObject, secondObject );
console.log( firstObject.foo ); // "bar"
console.log( newObject.foo ); // "baz"

link $.proxy()

Returns a function that will always run in the provided scope — that is, sets the meaning of this inside the passed function to the second argument.

1
2
3
4
5
6
7
8
9
10
11
12
var myFunction = function() {
console.log( this );
};
var myObject = {
foo: "bar"
};
myFunction(); // window
var myProxyFunction = $.proxy( myFunction, myObject );
myProxyFunction(); // myObject

If you have an object with methods, you can pass the object and the name of a method to return a function that will always run in the scope of the object.

1
2
3
4
5
6
7
8
var myObject = {
myFn: function() {
console.log( this );
}
};
$( "#foo" ).click( myObject.myFn ); // HTMLElement #foo
$( "#foo" ).click( $.proxy( myObject, "myFn" ) ); // myObject

link Testing Type

Sometimes the typeof operator can be confusing or inconsistent, so instead of using typeof, jQuery offers utility methods to help determine the type of a value.

First of all, you have methods to test if a specific value is of a specific type.

1
2
3
$.isArray([]); // true
$.isFunction(function() {}); // true
$.isNumeric(3.14); // true

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$.type( true ); // "boolean"
$.type( 3 ); // "number"
$.type( "test" ); // "string"
$.type( function() {} ); // "function"
$.type( new Boolean() ); // "boolean"
$.type( new Number(3) ); // "number"
$.type( new String('test') ); // "string"
$.type( new Function() ); // "function"
$.type( [] ); // "array"
$.type( null ); // "null"
$.type( /test/ ); // "regexp"
$.type( new Date() ); // "date"

As always, you can check the API docs for a more in-depth explanation.