0% found this document useful (0 votes)
48 views

Jquery Inrerview Questions

The document contains answers to 40 questions about jQuery. Some key points: - jQuery is a JavaScript library that makes HTML document traversal and manipulation, event handling, animation, and Ajax easier. It works by selecting elements and performing actions on the DOM. - The $ sign is an alias for jQuery. jQuery code execution starts with the $(document).ready() function, which runs when the DOM is loaded. - Common selectors include name, ID, class, and attribute selectors. ID and element selectors are fastest while class selectors are slower. - Common methods include animate() for animations, each() for iteration, parent() and parents() for traversing the DOM tree,

Uploaded by

Tyler
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Jquery Inrerview Questions

The document contains answers to 40 questions about jQuery. Some key points: - jQuery is a JavaScript library that makes HTML document traversal and manipulation, event handling, animation, and Ajax easier. It works by selecting elements and performing actions on the DOM. - The $ sign is an alias for jQuery. jQuery code execution starts with the $(document).ready() function, which runs when the DOM is loaded. - Common selectors include name, ID, class, and attribute selectors. ID and element selectors are fastest while class selectors are slower. - Common methods include animate() for animations, each() for iteration, parent() and parents() for traversing the DOM tree,

Uploaded by

Tyler
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Jquery Interview Questions

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

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

Q4. Is jQuery replacement of Java Script?

Ans: 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. 

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

Ans. Client side scripting.

Q6. Is jQuery a W3C standard?

Ans: No. jQuery is not a W3C standard.

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

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

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

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

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

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

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

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

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

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

Ans: Yes.

Q13. 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 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();
});

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

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

1. We can have more than one document.ready() function in a page where we can have only one body
onload function.
2. document.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.
Q15. What is the difference between .js and .min.js?

Ans: jQuery library comes in 2 different versions Production and Deployment. The deployment version is also known
as minified version. So .min.js is basically the minified version of 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.

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

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

Ans: There are 3 popular jQuery CDNs.

1. Google.
2. Microsoft
3. jQuery.

Advantage of using CDN.

 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

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

Q26. What are the fastest selectors in jQuery?

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


Q27. What are the slow selectors in jQuery?

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

Q28. How jQuery selectors are 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".

$("p#elmID .myCssClass");

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

Q30. 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
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);
});
});

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

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

$(document).ready(function(){
if ($('#element').is(':empty')){
//Element is empty
}
});

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


$(document).ready(function(){
if($.trim($('#element').html())=='') {
//Element is empty
}
});

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

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

$(document).ready(function(){
if ($('#element').length > 0){
//Element exists
});
});

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

Q34. 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
thesize() method as jQuery provide .length property and which does the same thing. But the .lengthproperty is
preferred because it does not have the overhead of a function call. 

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

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

Q37. 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 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 morehere.

Q38. 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:

(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"});
});

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

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

Ans: Using jQuery ".stop()" method.

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

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

.bind(): 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
future element. Above that it also has performance issues when dealing with a large selection.

.live(): 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.

.delegate(): 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.

Q46. What is 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 parse 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 defines 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

Q47. 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.
Q49. 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.

Q50. What is the difference between event.stopPropagation and


event.stopImmediatePropagation?

Ans: event.stopPropagation() allows other handlers on the same element to be executed,


whileevent.stopImmediatePropagation()  prevents every event from running. For example, see below jQuery code
block.

$("p").click(function(event){
event.stopImmediatePropagation();
});
$("p").click(function(event){
// This function won't be executed
$(this).css("background-color", "#f00");
});

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.

Q53. How do you attach a event to element which should be executed only once?

Ans: Using jQuery one() method. This attaches a handler to an event for the element. The handler is executed at
most once per element. In simple terms, the attached function will be called only once.

$(document).ready(function() {
$("#btnDummy").one("click", function() {
alert("This will be displayed only once.");
});
});

Q54. Can you include multiple version of jQuery? If yes, then how they are
executed?

Ans: Yes. Multiple versions of jQuery can be included in same page.

Q56. Is it possible to hold or delay document.ready execution for sometime?

Ans: Yes, its possible. With Release of jQuery 1.6, a new method " jQuery.holdReady(hold)" was introduced. This
method allows to delay the execution of document.ready() event. document.ready()event is called as soon as your
DOM is ready but sometimes there is a situation when you want to load additional JavaScript or some plugins which
you have referenced. 

$.holdReady(true);
$.getScript("myplugin.js", function() {
$.holdReady(false);
});

Q57. What is chaining in jQuery?

Ans: Chaining is one of the most powerful feature of jQuery. In jQuery, Chaining means to connect multiple
functions, events on selectors. It makes your code short and easy to manage and it gives better performance. The
chain starts from left to right. So left most will be called first and so on.

$(document).ready(function(){
$('#dvContent').addClass('dummy');
$('#dvContent').css('color', 'red');
$('#dvContent').fadeIn('slow');
});

The above jQuery code sample can be re-written using chaining. See below.

$(document).ready(function(){
$('#dvContent').addClass('dummy')
.css('color', 'red')
.fadeIn('slow');
});

Not only functions or methods, chaining also works with events in jQuery.

Q58. How does caching helps and how to use caching in jQuery? 

Ans: Caching is an area which can give you awesome performance, if used properly and at the right place. While
using jQuery, you should also think about caching. For example, if you are using any element in jQuery more than
one time, then you must cache it. See below code.

$("#myID").css("color", "red");
//Doing some other stuff......
$("#myID").text("Error occurred!");

Now in above jQuery code, the element with #myID is used twice but without caching. So both the times jQuery had
to traverse through DOM and get the element. But if you have saved this in a variable then you just need to
reference the variable. So the better way would be,

var $myElement = $("#myID").css("color", "red");


//Doing some other stuff......
$myElement.text("Error occurred!");

So now in this case, jQuery won't need to traverse through the whole DOM tree when it is used second time. So in
jQuery, Caching is like saving the jQuery selector in a variable. And using the variable reference when required
instead of searching through DOM again.
Q62. What are various methods to make ajax request in jQuery?

Ans: Using below jQuery methods, you can make ajax calls.

 load() : Load a piece of html into a container DOM


 $.getJSON(): Load JSON with GET method.
 $.getScript(): Load a JavaScript file.
 $.get(): Use to make a GET call and play extensively with the response.
 $.post(): Use to make a POST call and don't want to load the response to some container DOM.
 $.ajax(): Use this to do something on XHR failures, or to specify ajax options (e.g. cache: true) on the fly.

Q63. Is there any advantage of using $.ajax() for ajax call against $.get() or $.post()?

Ans: By using jQuery post()/ jQuery get(), you always trust the response from the server and you believe it is
going to be successful all the time. Well, it is certainly not a good idea to trust the response. As there can be n
number of reason which may lead to failure of response. 

Where jQuery.ajax() is jQuery's low-level AJAX implementation. $.get and $.post are higher-level abstractions


that are often easier to understand and use, but don't offer as much functionality (such as error callbacks).

Q65. Can we execute/run multiple Ajax request simultaneously in jQuery? If yes,


then how?

Ans: Yes, it is possible to execute multiple Ajax request simultaneously or in parallel. Instead of waiting for first ajax
request to complete and then issue the second request is time consuming. The better approach to speed up things
would be to execute multiple ajax request simultaneously.

Using jQuery .when() method which provides a way to execute callback functions based on one or more objects,
usually Deferred objects that represent asynchronous events.

Q75. Consider a scenario where things can be done easily with javascript, would you
still prefer jQuery?

Ans: No. If things can be done easily via CSS or JavaScript then You should not think about jQuery. Remember,
jQuery library always comes with xx kilobyte size and there is no point of wasting bandwidth.

Q76. Can we use protocol less URL while referencing jQuery from CDNs?

Ans: Yes. Below code is completely valid.

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


Q77. What is the advantage of using protocol less URL while referencing jQuery
from CDNs?

Ans: It is quite useful when you are moving from HTTP to HTTPS url. You need to make sure that correct protocol is
used for referencing jQuery library as pages served via SSL should contain no references to content served through
unencrypted connections.

"protocol-less" URL is the best way to reference third party content that’s available via both HTTP and HTTPS. When
a URL’s protocol is omitted, the browser uses the underlying document’s protocol instead. 

Q78. What is jQuery plugin and what is the advantage of using plugin?

Ans: A plug-in is piece of code written in a standard JavaScript file. These files provide useful jQuery methods which
can be used along with jQuery library methods. jQuery plugins are quite useful as its piece of code which is already
written by someone and re-usable, which saves your development time.

Q79. What is jQuery UI?

Ans: jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery
JavaScript Library that can be used to build interactive web applications.

Q80. What is the difference between jQuery and jQuery UI?

Ans: jQuery is the core library. jQueryUI is built on top of it. If you use jQueryUI, you must also include jQuery.

You might also like