Interview Q&A · questions with model answers

jQuery Interview Questions

Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.

  1. Most asked What is the difference between $(document).ready(), $(fn), DOMContentLoaded and window.onload?

    $(fn) (the recommended form of $(document).ready(fn)) runs when the DOM is parsed, like DOMContentLoaded, before images and stylesheets finish loading. window.onload (or $(window).on('load', fn))…

  2. Most asked What is the difference between .html() and .text(), and why does it matter for security?

    .html(value) parses the value as HTML and inserts the resulting elements; .text(value) inserts it as plain text, escaping < and &. Passing user-controlled data to .html() (or .append(), .prepend(),…

  3. Most asked What is the difference between preventDefault(), stopPropagation() and returning false in a jQuery handler?

    event.preventDefault() stops the browser's default action (following a link, submitting a form); event.stopPropagation() stops the event bubbling to ancestor handlers; returning false from a jQuery…

  4. Most asked What is the difference between .attr() and .prop()?

    .attr() reads and writes HTML attributes (the markup); .prop() reads and writes DOM properties (the live state of the element). For state such as checked, selected, disabled and value, use .prop():…

  5. Most asked What is the difference between .remove(), .detach() and .empty(), and how do you avoid memory leaks?

    .remove() removes elements and also their jQuery data and event handlers; .detach() removes elements but keeps data and handlers so you can reinsert them; .empty() removes all child nodes (and their…

  6. Most asked Which security issues have affected jQuery, and how do you protect a legacy site?

    Notable issues: XSS through HTML passed to DOM-manipulation methods in versions before 3.5.0 (CVE-2020-11022 and CVE-2020-11023, around htmlPrefilter), prototype pollution in $.extend(true, ...)…

  7. Most asked Can jQuery be used together with React or Vue?

    Yes, but carefully: the framework assumes it owns its DOM, so jQuery must not modify elements the framework renders, or the framework will overwrite the changes or crash during updates. The safe…

  8. How do jQuery Deferreds differ from native Promises, and what changed in jQuery 3?

    Before jQuery 3, Deferreds were not Promises/A+ compliant: .then() callbacks could run synchronously, and an exception thrown in a callback propagated instead of turning into a rejection. jQuery 3…

  9. jQuery animations or CSS transitions: which should you use?

    Prefer CSS transitions and animations (triggered by toggling classes) for most effects: browsers can run transform and opacity animations on the compositor, keeping them smooth. jQuery's .animate(),…

  10. What is the difference between $.each(), .each() and .map()?

    .each() iterates over the elements of a jQuery object (this is the DOM element); $.each(collection, fn) iterates over any array or object, with (index, value) or (key, value) arguments; .map()…

  11. How do .trigger() and .triggerHandler() differ, and how do you use custom events?

    .trigger('name', data) fires an event on the elements, runs jQuery handlers, bubbles up the DOM, and also performs the native default action where one exists (such as submit or focus).…

  12. What is the difference between this and $(this)?

    this is a plain DOM element (inside handlers and .each() callbacks); $(this) wraps it in a jQuery object so you can call jQuery methods. Native properties (this.value, this.checked, this.id) are…

Was this page helpful?

Report a mistake