JQuery Interview Questions and Answers

What is jQuery?

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 library and as per a survey it runs on every second website.

How JavaScript and jQuery are different?

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

Is jQuery replacement of Java Script?

No. jQuery is not a replacement of 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.

Is jQuery a library for client scripting or server scripting?

Client side scripting.

Is jQuery a W3C standard?

No. jQuery is not a W3C standard.

What is the basic need to start with jQuery?

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

Which is the starting point of code execution in jQuery?

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

What does dollar sign ($) means in jQuery?

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

$(document).ready(function(){
});

Over here $ sign can be replaced with "jQuery" keyword.

jQuery(document).ready(function(){
});

 

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

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

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

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

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

Yes it possible to use other client side libraries.

What is jQuery.noConflict?

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 other library as their global function. To overcome from such situations, jQuery has introduced jQuery.noConflict().

jQuery.noConflict();
// Use jQuery via jQuery(...)
jQuery(document).ready(function(){
   jQuery("div").hide();
}); 

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

var $j = jQuery.noConflict();
// Use jQuery via jQuery(...)
$j(document).ready(function(){
   $j("div").hide();
}); 

 

Why there are two different version of jQuery library?

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 development version. But the deployment version is 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.

Why do we use jQuery?

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.
• 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.

What is a CDN and What are the advantages of using CDN?

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. and 

  • It reduces the load from 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
  • Fast browsing experience for the users
  • Reduces the resources usage of your parent host, because the bandwidth eating media and theme files are served through CDN
  • Low connectivity latency because of the distributed system of servers

 There are 3 popular jQuery CDNs.

  1. Google.
  2. Microsoft
  3. jQuery

 

How to load jQuery from CDN?

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

<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

Code to load jQuery Framework from Microsoft CDN

<script type="text/javascript"
    src="http://ajax.microsoft.com/ajax/jquery/jquery-1.9.1.min.js">
</script>

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

<script type="text/javascript"
    src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>

 

What does $("div") will select?

This will select all the div elements on page.

What are the fastest selectors in jQuery?

ID and element selectors are the fastest selectors in jQuery.

What are the slow selectors in jQuery?

class selectors are the slow compare to ID and element.

How jQuery selectors are executed?

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".
$("p#elmID .myCssClass");

Difference between $(this) and 'this' in jQuery?

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

$(document).ready(function(){
    $('#spnValue').mouseover(function(){
       alert($(this).text());
  });
});

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.

$(document).ready(function(){
    $('#spnValue').mouseover(function(){
       alert(this.innerText);
  });
});

 

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

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.

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

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.

What is the difference between $('div') and $('<div/>') in jQuery?

$('<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.

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

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

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

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 retrieve 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. Find out more here.

How do you implement animation functionality?

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:
(selector).animate({styles},speed,easing,callback)
• 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. Default value is "swing".
• callback: Optional. A function to be executed after the animation completes.
Simple use of animate function is,

$("btnClick").click(function(){
  $("#dvBox").animate({height:"100px"});
});

 

How to disable jQuery animation?

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.