Jquery Interview Questions 2013 - DOTNET

Jquery Interview Questions 2013

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

Q2. 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.
·                     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.

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. Does jQuery follow W3C recommendations?
Ans: No.

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

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. 
1
$(document).ready(function(){
2
});
Over here $ sign can be replaced with "jQuery" keyword. 
1
jQuery(document).ready(function(){
2
});
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().
1
jQuery.noConflict();
2
// Use jQuery via jQuery(...)

3
jQuery(document).ready(function(){
4
   jQuery("div").hide();

5
});
You can also use your own specific character in the place of $ sign in jQuery.
1
var $j = jQuery.noConflict();
2
// Use jQuery via jQuery(...)

3
$j(document).ready(function(){
4
   $j("div").hide();

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

Q16. Why there are two different version of jQuery library?
Ans: jQuery library comes in 2 different versions. 

1.            Production
2.            Deployment
The production 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 production 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.

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.            1. Google.
2.            2. Microsoft
3.            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

Q19. 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
1
<script type="text/javascript"
2

3
</script>
Code to load jQuery Framework from Microsoft CDN
1
<script type="text/javascript"
2

3
</script>
Code to load jQuery Framework from jQuery Site(EdgeCast CDN)
1
<script type="text/javascript"
2

3
</script>
Q20. 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.
1
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
2
<script type="text/javascript">

3
if (typeof jQuery == 'undefined')
4
{

5
  document.write(unescape("%3Cscript src='Scripts/jquery.1.9.1.min.js' type='text/javascript'%3E%3C/script%3E"));
6
}

7
</script>
It first loads the jQuery from Google CDN and then check the jQuery object. If jQuery is not loaded successfully then it will references the jQuery.js file from hard drive location. In this example, the jQuery.js is loaded from Scripts folder.

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.

Q22. 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", then syntax would be,
1
$('#txtName')
Q23. What does $("div") will select?
Ans: This will select all the div elements on page.

Q24. How to select element having a particular class (".selected")?
Ans: $('.selected'). This selector is known as class selector. We need to prefix the class name with "." (dot).

Q25. What does $("div.parent") will select?
Ans: All the div element with parent class.

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".
1
$("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.
1
$(document).ready(function(){
2
    $('#spnValue').mouseover(function(){

3
       alert($(this).text());
4
  });

5
});
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
$(document).ready(function(){
2
    $('#spnValue').mouseover(function(){

3
       alert(this.innerText);
4
  });

5
});

Copyright © 2015 DOTNET All Right Reserved