anusha(salesforce developer)

jQuery Q&A

1.What is jQuery?
jQuery is a fast and concise JavaScript Library created by John Resig in 2006 with a nice motto – Write less, do more. jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is a JavaScript toolkit designed to simplify various tasks by writing less code.
2.What are the core features of jQuery?
Following is the list of important core features supported by jQuery −
DOM manipulation − The jQuery made it easy to select DOM elements, traverse them and modifying their content by using cross-browser open source selector engine called Sizzle.
Event handling − The jQuery offers an elegant way to capture a wide variety of events, such as a user clicking on a link, without the need to clutter the HTML code itself with event handlers.
AJAX Support − The jQuery helps you a lot to develop a responsive and feature-rich site using AJAX technology.
Animations − The jQuery comes with plenty of built-in animation effects which you can use in your websites.
Lightweight − The jQuery is very lightweight library – about 19KB in size ( Minified and gzipped ).
Cross Browser Support − The jQuery has cross-browser support, and works well in IE 6.0+, FF 2.0+, Safari 3.0+, Chrome and Opera 9.0+.
Latest Technology − The jQuery supports CSS3 selectors and basic XPath syntax.
3.How will you make sure that DOM is ready using jQuery?
Use $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.
4.How can you create an Object in JavaScript?
JavaScript supports Object concept very well. You can create an object using the object literal as follows −
var emp = {
name: “Zara”,
age: 10
};
5.How can you read properties of an Object in JavaScript?
You can write and read properties of an object using the dot notation as follows −
// Getting object properties
emp.name // ==> Zara
emp.age // ==> 10
// Setting object properties
emp.name = “Daisy” // <== Daisy
emp.age = 20 // <== 20
6.How can you create an Array in JavaScript?
You can define arrays using the array literal as follows −
var x = [];
var y = [1, 2, 3, 4, 5];
7.How to read elements of an array in JavaScript?
An array has a length property that is useful for iteration. We can read elements of an array as follows −
var x = [1, 2, 3, 4, 5];
for (var i = 0; i < x.length; i++) {
// Do something with x[i]
}
8.What is a named function in JavaScript? How to define a named function?
A named function has a name when it is defined. A named function can be defined using function keyword as follows −
function named(){
// do some stuff here
}
9.How many types of functions JavaScript supports?
A function in JavaScript can be either named or anonymous.
10.How to define a anonymous function?
An anonymous function can be defined in similar way as a normal function but it would not have any name. 
11.Can you assign a anonymous function to a variable?
Yes! An anonymous function can be assigned to a variable.
12.Can you pass a anonymous function as an argument to another function?
Yes! An anonymous function can be passed as an argument to another function.
13.What is arguments object in JavaScript?
JavaScript variable arguments represents the arguments passed to a function.
14.How can you get the type of arguments passed to a function?
Using typeof operator, we can get the type of arguments passed to a function. For example −
function func(x){
console.log(typeof x, arguments.length);
}
func(); //==> “undefined”, 0
func(1); //==> “number”, 1
func(“1”, “2”, “3”); //==> “string”, 3
15.How can you get the total number of arguments passed to a function?
Using arguments.length property, we can get the total number of arguments passed to a function. For example −
function func(x){
console.log(typeof x, arguments.length);
}
func(); //==> “undefined”, 0
func(1); //==> “number”, 1
func(“1”, “2”, “3”); //==> “string”, 3
16.How can you get the reference of a caller function inside a function?
The arguments object has a callee property, which refers to the function you’re inside of. For example −
function func() {
return arguments.callee;
}
func(); // ==> func
17.What is the purpose of ‘this’ operator in JavaScript?
JavaScript famous keyword this always refers to the current context.
18.What are the valid scopes of a variable in JavaScript?
The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.
Global Variables − A global variable has global scope which means it is visible everywhere in your JavaScript code.
Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
19.Which type of variable among global and local, takes precedence over other if names are same?
A local variable takes precedence over a global variable with the same name.
20.What is callback?
A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered. (150)
21.What is closure?
Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.
22.Give an example of closure?
Following example shows how the variable counter is visible within the create, increment, and print functions, but not outside of them −
function create() {
var counter = 0;
return {
increment: function() {
counter++;
},
print: function() {
console.log(counter);
}
}
}
var c = create();
c.increment();
c.print(); // ==> 1
23.Which built-in method returns the character at the specified index?
charAt() method returns the character at the specified index.
24.Which built-in method combines the text of two strings and returns a new string?
concat() method returns the character at the specified index.
25.Which built-in method calls a function for each element in the array?
forEach() method calls a function for each element in the array.
26.Which built-in method returns the index within the calling String object of the first occurrence of the specified value?
indexOf() method returns the index within the calling String object of the first occurrence of the specified value, or −1 if not found.
27.Which built-in method returns the length of the string?
length() method returns the length of the string.
28.Which built-in method removes the last element from an array and returns that element?
pop() method removes the last element from an array and returns that element.
29.Which built-in method adds one or more elements to the end of an array and returns the new length of the array?
push() method adds one or more elements to the end of an array and returns the new length of the array.
30.Which built-in method reverses the order of the elements of an array?
reverse() method reverses the order of the elements of an array — the first becomes the last, and the last becomes the first. (152)
31.Which built-in method sorts the elements of an array?
sort() method sorts the elements of an array.
32.Which built-in method returns the characters in a string beginning at the specified location?
substr() method returns the characters in a string beginning at the specified location through the specified number of characters.
33.Which built-in method returns the calling string value converted to lower case?
toLowerCase() method returns the calling string value converted to lower case.
34.Which built-in method returns the calling string value converted to upper case?
toUpperCase() method returns the calling string value converted to upper case.
35.Which built-in method returns the string representation of the number’s value?
toString() method returns the string representation of the number’s value.
36.What is a jQuery selector?
A jQuery Selector is a function which makes use of expressions to find out matching elements from a DOM based on the given criteria. Simply you can say, selectors are used to select one or more HTML elements using jQuery. Once an element is selected then we can perform various operations on that selected element. jQuery selectors start with the dollar sign and parentheses – $().
37.How to resolve confict with another JavaScript library if $ is already being in use?
The factory function $() is a synonym of jQuery() function. So in case you are using any other JavaScript library where $ sign is conflicting with some thing else then you can replace $ sign by jQuery name and you can use function jQuery() instead of $().
38.How to select elements using jQuery with the given element tag-name?
$(‘tag-name’) selects all element of type tag-name in the document. For example, $(‘p’) selects all paragraphs <p> in the document.
39.How to select single element using jQuery with the given element id some-id?
$(‘#some-id’) selects the single element in the document that has an ID of some-id.
40.How to select elements using jQuery whose css class is some-class?
$(‘.some-class’) selects all elements in the document that have a class of some-class.
41.How to select all elements using jQuery?
$(‘*’) selects all elements available in a DOM.
42.How to select multiple elements using jQuery?
$(‘E, F, G’) selects the combined results of all the specified selectors E, F or G where selectors can be any valid jQuery selector.
43.How to get attributes of an element using jQuery?
The attr() method can be used to fetch the value of an attribute from the first element in the matched set.
44.How to set attributes of an element using jQuery?
The attr(name, value) method can be used to set the named attribute onto all elements in the wrapped set using the passed value.
45.How can you apply a style on an element using jQuery?
The addClass( classes ) method can be used to apply defined style sheets onto all the matched elements. You can specify multiple classes separated by space.
46.How to remove an attribute from each of the matched elements using jQuery?
The removeAttr( name ) method can be used to remove an attribute from each of the matched elements.
47.How to know if a specified class is present on at least one of the set of matched elements using jQuery?
The hasClass( class ) method returns true if the specified class is present on at least one of the set of matched elements.
48.How to remove all or the specified class(es) from the set of matched elements using jQuery?
The removeClass(class) method remove all or the specified class(es) from the set of matched elements.
49.How to add the specified class if it is not present, remove the specified class if it is present using jQuery?
The toggleClass(class) method adds the specified class if it is not present, removes the specified class if it is present.
50.How to get the html contents (innerHTML) of an element using jQuery?
The html() method gets the html contents (innerHTML) of the first matched element. (163)
================================================================
1. 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.
2. Why do we use jQuery?
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.
 3. 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.
4. 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.
5. Is jQuery a library for client scripting or server scripting?
Client side scripting.
6. Is jQuery a W3C standard?
No. jQuery is not a W3C standard.
7. 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.
8. 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.
9. 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.
Hide   Copy Code
$(document).ready(function(){
});
Over here $ sign can be replaced with “jQuery” keyword.
Hide   Copy Code
jQuery(document).ready(function(){
});
10. 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. (94)

No comments:

Post a Comment