jQuery Interview questions for experienced Candidates

Dear Reader, As we know that, jQuery is so much interesting and very attractive to all developers, as it has very flexible and stunning functionalities with very quick and shortcode requirement for developing a very big and required functionalities. Like if you want to play with AJAX then you will prefer the jQuery in the place of JavaScript for sure.

Let us see the code snippet:-

AJAX call in JavaScript:-

AJAX call in jQuery:-

Very sweet and simple code snippet in jQuery.

Although we will give more description about AJAX in next article. And now let us concentrate on Interview questions, so here we go:-

  1. What is jQuery?

Ans: jQuery is fast, lightweight and feature-rich client-side JavaScript Library/Framework which helps in to traverse HTML DOM, make animations, add Ajax interaction, manipulate the page content, change the style and provide cool UI effect. It is one of the most popular client-side libraries and as per a survey it runs on every second website.

  1. Why do we use jQuery?

Ans: Due to following advantages.

  • Easy to use and learn.
  • Easily expandable.
  • Cross-browser support (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)
  • Easy to use for DOM manipulation and traversal.
  • The large pool of built-in methods.
  • AJAX Capabilities.
  • Methods for changing or applying CSS, creating animations.
  • Event detection and handling.
  • Tons of plug-ins for all kind of needs.


  1. How JavaScript and jQuery are different?

Ans: JavaScript is a language While jQuery is a library built in the JavaScript language that helps to use the JavaScript language.

  1. Explain what the following code will do:

$( “div#first, div.first, ol#items > [name$=’first’]” )

Ans: This code performs a query to retrieve any

<div> element with the id first, plus all <div> elements with the class first, plus all elements which are children of the <ol id=”items”> element and whose name attribute ends with the string “first”. This is an example of using multiple selectors at once. The function will return a jQuery object containing the results of the query.

  1. Is jQuery replacement of JavaScript?

Ans: No. jQuery is not a replacement for JavaScript. jQuery is a different library which is written on top of JavaScript. jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML.

  1. What’s the deal with the $in jQuery? What is it and what does it mean?Also, how can jQuery be used in conjunction with another JavaScript library that also uses $for naming? Bonus credit if you can provide two answers.

Ans: Since $ has no special meaning in JavaScript, it is free to be used in object naming. In jQuery, it is simply used as an alias for the jQuery object and jQuery() function.

However, jQuery has no monopoly on the use of $, so you may encounter situations where you want to use it in conjunction with another JS library that also uses $, which would, therefore, result in a naming conflict. jQuery provides the jQuery.noConflict()method for just this reason. Calling this method makes it necessary to use the underlying name jQuery instead of subsequent references to jQuery and its functions.

Here’s an example from the jQuery documentation:

Alternatively, you can also use a closure instead of the $.noConflict() method; e.g.:

  1. Is jQuery a library for client scripting or server scripting?

Ans. Client-side scripting.

  1. Is jQuery a W3C standard?

Ans: No. jQuery is not a W3C standard.

  1. What is the basic need to start with jQuery?

Ans: To start with jQuery, one need to make reference of it’s library. The latest version of jQuery can be downloaded from jQuery.com.

  1. Which is the starting point of code execution in jQuery?

Ans: The starting point of jQuery code execution is $(document).ready() function which is executed when DOM is loaded.

  1. What does dollar sign ($) means in jQuery?

Ans: Dollar Sign is nothing but it’s an alias for JQuery. Take a look at below jQuery code.

Over here $ sign can be replaced with “jQuery” keyword.

  1. Can we have multiple documents.ready() function on the same page?

Ans: YES. We can have any number of document.ready() function on the same page.

  1. Given the following HTML:

        <div id=”expander”></div>

       and the following CSS:

       div#expander{ width: 100px; height: 100px; background-color: blue; }

       Write code in jQuery to animate the #expander div, expanding it from 100 x 100 pixels to

       200 x 200 pixels  over the course of three seconds.

Ans: This could be done in jQuery as follows:

  1. Can we use our own specific character in the place of $ sign in jQuery?

Ans: Yes. It is possible using jQuery.noConflict().

  1. Is it possible to use other client-side libraries like MooTools, Prototype along with jQuery?

Ans: Yes.

  1. What is jQuery.noConflict?

Ans: As other client-side libraries like MooTools, Prototype can be used with jQuery and they also use $() as their global function and to define variables. This situation creates conflict as $() is used by jQuery and another library as their global function. To overcome from such situations, jQuery has introduced jQuery.noConflict().

You can also use your own specific character in the place of $ sign in jQuery.

  1. Is there any difference between body onload() and document.ready() function?

Ans: document.ready() function is different from body onload() function for 2 reasons.

  • We can have more than one ready()function in a page where we can have only one body onload function.
  • ready()function is called as soon as DOM is loaded where body.onload() function is called when everything gets loaded on the page that includes DOM, images and all associated resources of the page.
  1. What is the difference between .js and .min.js?

Ans: jQuery library comes in 2 different versions Development and Production/Deployment. The deployment version is also known as the minified version. So .min.js is basically the minified version of the jQuery library file. Both the files are same as far as functionality is concerned. but .min.js is quite small in size so it loads quickly and saves bandwidth.

  1. Why there are two different version of the jQuery library?

Ans: jQuery library comes in 2 different versions.

  1. Development
  2. Production/Deployment

The development version is quite useful at development time as jQuery is open source and if you want to change something then you can make those changes in the development version. But the deployment version is the minified version or compressed version so it is impossible to make changes in it. Because it is compressed, so its size is very less than the production version which affects the page load time.

  1. What is a CDN?

Ans: A content delivery network or content distribution network (CDN) is a large distributed system of servers deployed in multiple data centers across the Internet. The goal of a CDN is to serve content to end-users with high availability and high performance.

  1. Which are the popular jQuery CDN? and what is the advantage of using CDN?

Ans: There are 3 popular jQuery CDN.

  1. Google.
  2. Microsoft
  3. jQuery.

An advantage of using CDN.

  • It reduces the load on your server.
  • It saves bandwidth. jQuery framework will load faster from these CDN.
  • The most important benefit is it will be cached if the user has visited any site which is using jQuery framework from any of these CDN
  1. How to load jQuery from CDN?

Ans: Below is the code to load jQuery from all 3 CDNs.
Code to load jQuery Framework from Google CDN

Code to load jQuery Framework from Microsoft CDN

Code to load jQuery Framework from jQuery Site(EdgeCast CDN)

  1. How to load jQuery locally when CDN fails?

Ans: It is a good approach to always use CDN but sometimes what if the CDN is down (rare possibility though) but you never know in this world as anything can happen.

Below given jQuery code checks whether jQuery is loaded from Google CDN or not, if not then it references the jQuery.js file from your folder.

It first loads the jQuery from Google CDN and then checks the jQuery object. If jQuery is not loaded successfully then it will reference the jQuery.js file from hard drive location. In this example, the jQuery.js is loaded from Scripts folder.

  1. What are selectors in jQuery and how many types of selectors are there?

Ans: To work with an element on the web page, first we need to find them. To find the HTML element in jQuery we use selectors. There are many types of selectors but basic selectors are:

  • Name: Selects all elements which match with the given element Name.
  • #ID: Selects a single element which matches with the given ID
  • .Class: Selects all elements which match with the given Class.
  • Universal (*): Selects all elements available in a DOM.
  • Multiple Elements E, F, G: Selects the combined results of all the specified selectors E, F or G.
  • Attribute Selector: Select elements based on its attribute value.
  1. How do you select element by ID in jQuery?

Ans: To select element use ID selector. We need to prefix the id with “#” (hash symbol). For example, to select element with ID “txtName”, the syntax would be,

  1. What does $(“div”) will select?

Ans: This will select all the div elements on the page.

  1. How to select element having a particular class (“.selected”)?

Ans: $(‘.selected’). This selector is known as a class selector. We need to prefix the class name with “.” (dot).

  1. What does $(“div.parent”) will select?

Ans: All the div element with the parent class.

  1. What are the fastest selectors in jQuery?

Ans: ID and element selectors are the fastest selectors in jQuery.

  1. What are the slow selectors in jQuery?

Ans: class selectors are the slow compared to ID and element.

  1. How are jQuery selectors executed?

Ans: Your last selectors is always executed first. For example, in below jQuery code, jQuery will first find all the elements with class “.myCssClass” and after that it will reject all the other elements which are not in “p#elmID”.

  1. Which is fast document.getElementByID(‘txtName’) or $(‘#txtName’).?

Ans: Native JavaScipt is always fast. jQuery method to select txtName “$(‘#txtName’)” will internally makes a call to document.getElementByID(‘txtName’). As jQuery is written on top of JavaScript and it internally uses JavaScript only So JavaScript is always fast.

  1. Difference between $(this) and ‘this’ in jQuery?

Ans: this and $(this) refers to the same element. The only difference is the way they are used. ‘this’ is used in the traditional sense, when ‘this’ is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.

In below example, this is an object but since it is not wrapped in $(), we can’t use jQuery method and use the native JavaScript to get the value of span element.

  1. How do you check if an element is empty?

Ans: There are 2 ways to check if an element is empty or not. We can check using “: empty” selector.

And the second way is using the “$.trim()” method.

  1. How do you check if an element exists or not in jQuery?

Ans: Using jQuery length property, we can ensure whether an element exists or not.

  1. What is the use of jquery .each() function?

Ans: The $.each() function is used to iterate over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array.

  1. What is the difference between jquery.size() and jquery.length?

Ans: jQuery .size() method returns number of element in the object. But it is not preferred to use the size()method as jQuery provide .length property and which does the same thing. But the .length property is preferred because it does not have the overhead of a function call.

  1. What is the difference between $(‘div’) and $(‘<div/>’) in jQuery?

Ans: $(‘<div/>’) : This creates a new div element. However this is not added to DOM tree unless you don’t append it to any DOM element.

$(‘div’) : This selects all the div element present on the page.

  1. What is the difference between parent() and parents() methods in jQuery?

Ans: The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree.

  1. What is the difference between eq() and get() methods in jQuery?

Ans: eq() returns the element as a jQuery object. This method constructs a new jQuery object from one element within that set and returns it. That means that you can use jQuery functions on it.

get() return a DOM element. The method retrieves the DOM elements matched by the jQuery object. But as it is a DOM element and it is not a jQuery-wrapped object. So jQuery functions can’t be used.

  1. How do you implement animation functionality?

Ans: The .animate() method allows us to create animation effects on any numeric CSS property. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect.

Syntax is:


  • styles: Specifies one or more CSS properties/values to animate.
  • duration: Optional. Specifies the speed of the animation.
  • easing: Optional. Specifies the speed of the element in different points of the animation. The default value is “swing”.
  • callback: Optional. A function to be executed after the animation completes.

Simple use of the animate function is,

  1. How to disable jQuery animation?

Ans: Using jQuery property “jQuery.fx.off“, which when set to true, disables all the jQuery animation. When this is done, all animation methods will immediately set elements to their final state when called, rather than displaying an effect.

  1. How do you stop the currently-running animation?

Ans: Using jQuery “.stop()” method.

  1. What is the difference between .empty(), .remove() and .detach() methods in jQuery?

Ans: All these methods .empty(), .remove() and .detach() are used for removing elements from DOM but they all are different.

.empty(): This method removes all the child element of the matched element where remove() method removes set of matched elements from DOM.

.remove(): Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

.detach(): This method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

  1. Explain .bind() vs .live() vs .delegate() vs .on()

Ans: All these 4 jQuery methods are used for attaching events to selectors or elements. But they all are different from each other.

<a href=”http://jquerybyexample.blogspot.com/2010/06/jquery-bind-function-exampledemo.html”>.bind()</a>: This is the easiest and quick method to bind events. But the issue with bind() is that it doesn’t work for elements added dynamically that matches the same selector. bind() only attach events to the current elements, not a future element. Above that, it also has performance issues when dealing with a large selection.

<a href=”http://jquerybyexample.blogspot.com/2010/06/jquery-live-function-exampledemo.html”>.live()</a>: This method overcomes the disadvantage of bind(). It works for dynamically added elements or future elements. Because of its poor performance on large pages, this method is deprecated as of jQuery 1.7 and you should stop using it. Chaining is not properly supported using this method.

<a href=”http://jquerybyexample.blogspot.com/2010/08/jquery-delegate-function-exampledemo.html”>.delegate()</a>: The .delegate() method behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored and it also supports chaining.

.on(): Since live was deprecated with 1.7, so new method was introduced named “.on()”. This method provides all the goodness of previous 3 methods and it brings uniformity for attaching event handlers.

  1. What is wrong with this code line “$(‘#myid.3’).text(‘blah blah!!!’);”

Ans: The problem with above statement is that the selectors is having meta characters and to use any of the meta-characters ( such as !”#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. For example, an element with id=”foo.bar”, can use the selector $(“#foo\\.bar”).
So the correct syntax is,

  1. How to create a clone of any object using jQuery?

Ans: jQuery provides clone() method which performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes.

  1. Does events are also copied when you clone an element in jQuery?

Ans: As explained in the previous question, using clone() method, we can create a clone of any element but the default implementation of the clone() method doesn’t copy events unless you tell the clone() method to copy the events. The clone() method takes a parameter, if you pass true then it will copy the events as well.

  1. What is the difference between prop and attr?

Ans: attr(): Get the value of an attribute for the first element in the set of matched elements. Whereas,.prop(): (Introduced in jQuery 1.6) Get the value of a property for the first element in the set of matched elements.

Attributes carry additional information about an HTML element and come in name=”value” pairs. Where Property is a representation of an attribute in the HTML DOM tree. once the browser parses your HTML code , corresponding DOM node will be created which is an object thus having properties.

attr() gives you the value of element as it was defined in the HTML on page load. It is always recommended to use prop() to get values of elements which is modified via javascript/ jquery, as it gives you the original value of an element’s current state.

  1. What is event PreventDefault?

Ans: The event.preventDefault() method stops the default action of an element from happening. For example, Prevents a link from following the URL.

  1. What is the difference between event? PreventDefault and event.stopPropagation?

Ans: event.preventDefault(): Stops the default action of an element from happening.
event.stopPropagation(): Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. For example, if there is a link with a click method attached inside of a DIV or FORM that also has a click method attached, it will prevent the DIV or FORM click method from firing.

  1. What is the difference between event? PreventDefault and “return false”?

Ans: e.preventDefault() will prevent the default event from occurring, e.stopPropagation() will prevent the event from bubbling up and return false will do both.

  1. What is the difference between event.stopPropagation and event.stopImmediatePropagation?

Ans: event.stopPropagation() allows other handlers on the same element to be executed, while event.stopImmediatePropagation() prevents every event from running. For example, see below jQuery code block.

If event.stopPropagation was used in previous example, then the next click event on p element which changes the css will fire, but in case event.stopImmediatePropagation(), the next p click event will not fire.


Leave a comment:

Your email address will not be published. Required fields are marked *