jQuery Interview Questions Part 3

11. What is the use of .each() function in jQuery?

Answer:

The .each() function is used to iterate each and every element of an object.

It is used to loop DOM elements, arrays and the object properties.

This method specifies a function to run for each matched element.

Syntax:
$(selector).each(function(index,element))

Parameters,

function(index,element): This function is mainly required and used to run for each matched element.

index: It specifies the index position of the selector.

element: It specifies the current element.

This function can be used to iterate over any collection, whether it is an object or an array.

12. What is the use of load() method in jQuery?

Answer:

The load() method is a powerful AJAX method which is used to load the data from a server and assign the data into the element without loading the page.

Syntax:
$(selector).load(URL,data,callback);

Parameters,

URL: It specifies the URL you wish to load.

data: It specifies a set of querystring key/value pairs to send along with the request.

callback: It is a name of a function to be executed after the load() method is completed.

This method is the simplest way to fetch data from the server.

13. What is the use of jQuery filter?

Answer:

Filter is used to filter the certain values from the object list based on the criteria. For example, filter certain products from the master list of products in a cart website.

The .filter() method reduces the set of matched elements to those that match the selector or pass the function's test.

This method lets you specify a criteria. Elements that do not match the criteria are removed from the selection, and those that match will be returned.

Filter is often used to narrow down the search for an element in a group of selected elements.

Syntax:
$(selector).filter(criteria,function(index))

Parameters,

criteria: It specifies a selector expression, a jQuery object or one or more elements to be returned from a group of selected elements. To specify multiple criteria, use comma (,).

function(index): It specifies a function to run for each element in the set. If it returns true, the element is kept. Otherwise, the element is removed.

index: It specifies the index position of the element in the set.

14. What is CDN?

Answer:

CDN stands for Content Distribution network.

CDN is said to be a group of companies in different location with network containing copies of data files to maximize bandwidth in accessing the data.

It is a system of distributed servers (network) that deliver pages and other Web content to a user, based on the geographic locations of the user, the origin of the webpage and the content delivery server.

This service is effective in speeding the delivery of content of websites with high traffic and websites that have global reach.

The main goal of CDN is to provide content to the end-users with high availability and high performance.

15. What is the use of ajax() method in jQuery?

Answer:

AJAX stands for Asynchronous JavaScript and XML. AJAX loads data in the background and display it on the web page, without reloading the whole page.

jQuery provides several methods for AJAX functionality. Using jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and Post method. And also you can load the external data directly into the selected HTML elements of your web page.

AJAX coding is difficult without jQuery, because different browsers have different syntax for AJAX implementation. This means that you will have to write extra code to test for different browsers. To avoid this problem, jQuery has taken care of this, so that we can write AJAX functionality with a single line of code.

The ajax() method is used for asynchronous HTTP requests.

All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

Syntax:
$.ajax({name:value, name:value, ... })

The parameters specifies one or more name/value pairs for the AJAX request.