jQuery - Interview Questions



Dear readers, these jQuery Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of jQuery. As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer −

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.

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.

Use $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

JavaScript supports Object concept very well. You can create an object using the object literal as follows −

var emp = {
   name: "Zara",
   age: 10
};

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

You can define arrays using the array literal as follows −

var x = [];
var y = [1, 2, 3, 4, 5];

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]
}

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
}

A function in JavaScript can be either named or anonymous.

An anonymous function can be defined in similar way as a normal function but it would not have any name.

Yes! An anonymous function can be assigned to a variable.

Yes! An anonymous function can be passed as an argument to another function.

JavaScript variable arguments represents the 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

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

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

JavaScript famous keyword this always refers to the current context.

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.

A local variable takes precedence over a global variable with the same name.

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.

Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.

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

charAt() method returns the character at the specified index.

concat() method returns the character at the specified index.

forEach() method calls a function for each element in the array.

indexOf() method returns the index within the calling String object of the first occurrence of the specified value, or −1 if not found.

length() method returns the length of the string.

pop() method removes the last element from an array and returns that element.

push() method adds one or more elements to the end of an array and returns the new length of the array.

reverse() method reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.

sort() method sorts the elements of an array.

substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

toLowerCase() method returns the calling string value converted to lower case.

toUpperCase() method returns the calling string value converted to upper case.

toString() method returns the string representation of the number's value.

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

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

$('tag-name') selects all element of type tag-name in the document. For example, $('p') selects all paragraphs <p> in the document.

$('#some-id') selects the single element in the document that has an ID of some-id.

$('.some-class') selects all elements in the document that have a class of some-class.

$('*') selects all elements available in a DOM.

$('E, F, G') selects the combined results of all the specified selectors E, F or G where selectors can be any valid jQuery selector.

The attr() method can be used to fetch the value of an attribute from the first element in the matched set.

The attr(name, value) method can be used to set the named attribute onto all elements in the wrapped set using the passed value.

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.

The removeAttr( name ) method can be used to remove an attribute from each of the matched elements.

The hasClass( class ) method returns true if the specified class is present on at least one of the set of matched elements.

The removeClass(class) method remove all or the specified class(es) from the set of matched elements.

The toggleClass(class) method adds the specified class if it is not present, removes the specified class if it is present.

The html() method gets the html contents (innerHTML) of the first matched element.

The html( val ) method sets the html contents of every matched element.

The text( ) method gets the combined text contents of all matched elements.

The text( val ) sets the text contents of all matched elements.

The val( ) method gets the input value of the first matched element.

The val(val) method sets the value attribute of every matched element if it is called on <input> but if it is called on <select> with the passed <option> value then passed option would be selected, if it is called on check box or radio box then all the matching check box and radiobox would be checked.

The filter( selector ) method can be used to filter out all elements from the set of matched elements that do not match the specified selector(s). The selector can be written using any selector syntax.

The eq( index ) method reduces the set of matched elements to a single element.

The is( selector ) method checks the current selection against an expression and returns true, if at least one element of the selection fits the given selector.

The not(selector) method removes elements matching the specified selector from the set of matched elements.

The slice(selector) method selects a subset of the matched elements.

The add( selector ) method adds more elements, matched by the given selector, to the set of matched elements.

The andSelf( ) method adds the previous selection to the current selection.

The children( [selector]) method gets a set of elements containing all of the unique immediate children of each of the matched set of elements.

The closest( selector ) method gets a set of elements containing the closest parent element that matches the specified selector, the starting element included.

The contents( ) method finds all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe.

The end( ) method reverts the most recent 'destructive' operation, changing the set of matched elements to its previous state.

The find( selector ) method searches for descendent elements that match the specified selectors.

The next( [selector] ) gets a set of elements containing the unique next siblings of each of the given set of elements.

The nextAll( [selector] ) finds all sibling elements after the current element.

The offsetParent( ) method returns a jQuery collection with the positioned parent of the first matched element.

The parent( [selector] ) method gets the direct parent of an element. If called on a set of elements, parent returns a set of their unique direct parent elements.

The parents( [selector] ) method gets a set of elements containing the unique ancestors of the matched set of elements (except for the root element).

The prev( [selector] ) method gets a set of elements containing the unique previous siblings of each of the matched set of elements.

The prevAll( [selector] ) method finds all sibling elements in front of the current element.

The siblings( [selector] ) method gets a set of elements containing all of the unique siblings of each of the matched set of elements.

The css( name ) method returns a style property on the first matched element.

The css( name, value ) method sets a single style property to a value on all matched elements.

The height( val ) method sets the CSS height of every matched element.

The height( ) method gets the current computed, pixel, height of the first matched element.

The innerHeight( ) method gets the inner height (excludes the border and includes the padding) for the first matched element.

The innerWidth( ) method gets the inner width (excludes the border and includes the padding) for the first matched element.

The offset( ) method gets the current offset of the first matched element, in pixels, relative to the document.

The offsetParent( ) method returns a jQuery collection with the positioned parent of the first matched element.

The outerHeight( [margin] ) method gets the outer height (includes the border and padding by default) for the first matched element.

The outerWidth( [margin] ) method gets the outer width (includes the border and padding by default) for the first matched element.

The position( ) method gets the top and left position of an element relative to its offset parent.

The width( val ) method sets the CSS width of every matched element.

The width( ) method gets the current computed, pixel, width of the first matched element.

The empty( ) method removes all child nodes from the set of matched elements.

The remove( expr ) method removes all matched elements from the DOM.

The preventDefault() method of Event object prevents the browser from executing the default action.

The isDefaultPrevented() method of Event object returns whether event.preventDefault() was ever called on this event object.

The stopPropagation() method of Event object stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

The isPropagationStopped() method of Event object returns whether event.stopPropagation() was ever called on this event object.

The stopImmediatePropagation() method of Event object stops the rest of the handlers from being executed.

The isImmediatePropagationStopped() method of Event object returns whether event.stopImmediatePropagation() was ever called on this event object.

The bind( type, [data], fn ) function binds a handler to one or more events (like click) for each matched element. Can also bind custom events.

The ready(fn) function binds a function to be executed whenever the DOM is ready to be traversed and manipulated.

The load( url, [data], [callback] ) method load HTML from a remote file and inject it into the DOM.

The ajaxComplete( callback ) method can be used to register a callback to be executed whenever an AJAX request completes.

What is Next ?

Further you can go through your past assignments you have done with the subject and make sure you are able to speak confidently on them. If you are fresher then interviewer does not expect you will answer very complex questions, rather you have to make your basics concepts very strong.

Second it really doesn't matter much if you could not answer few questions but it matters that whatever you answered, you must have answered with confidence. So just feel confident during your interview. We at tutorialspoint wish you best luck to have a good interviewer and all the very best for your future endeavor. Cheers :-)

Advertisements