I was at an interview recently and although they knew that I was a beginner in javascript they asked me what selectors I used before? I didn't know what to say.

I came back home and searched for it but can't find a clear definition. Isn't a selector something like a .class or #id?

  • 8
    There are no "JavaScript selectors". Mar 5 '13 at 15:08

Yes, you were interviewed by morons. Consider that bullet dodged. They were probably thinking of jQuery, which you probably should learn, but it does in fact use CSS selectors and xpath... (whatever you call xpath statements/selector thingies) for syntax. The right answer if you should want to be hired by somebody who makes the same mistake again is "JavaScript doesn't have selectors. Are you talking about jQuery? Or are you referring to the DOM API?" The DOM API, btw, is something you should already know if you don't.

Edit: Okay, maybe not total morons. They could have been thinking of the query selector API (see Dystroy's answer) but IMO, that still should have been clear.

  • 2
    lol at silent down-vote. Maybe somebody who's asked that question? Mar 5 '13 at 15:07
  • 8
    @AAA: w3schools is a horrible reference. Please do not use it for anything! Mar 5 '13 at 15:08
  • 2
    The official jQuery docs: api.jquery.com/category/selectors Mar 5 '13 at 15:10
  • 5
    sigh w3schools is fine for beginners w3fools isnt even kept up to date as is, and is pretty pretentious. However MDN is a much better resource.
    – Loktar
    Mar 5 '13 at 15:10
  • 3
    @Loktar I would say w3schools is fine for reference if it happens to have the exact method you were looking for on a google but I wouldn't advise treating it as a primary source for beginners since it tends to be dated/incomplete. On the other hand, unlike MDN which does have good info, at least the info is always there and not awaiting some wiki upgrade or other form of content-blocking foolishness. Mar 5 '13 at 15:12

Your interviewer was probably referring to the query language which lets you form selectors to find elements in the DOM using methods like querySelectorAll.

Note that selectors are normalized by the w3.org in the selectors API :

Selectors, which are widely used in CSS, are patterns that match against elements in a tree structure [SELECT][CSS21]. The Selectors API specification defines methods for retrieving Element nodes from the DOM by matching against a group of selectors. It is often desirable to perform DOM operations on a specific set of elements in a document. These methods simplify the process of acquiring specific elements, especially compared with the more verbose techniques defined and used in the past.

The purpose of the question could be to check you know those selectors exist, which would make a difference with many developers only aware of the use of libraries like jQuery and who would make the mistake of answering that JavaScript doesn't have selectors.

  • 2
    That's my guess too, the new(ish) Selectors API is probably what they meant.
    – yannis
    Mar 5 '13 at 15:12
  • Good point. But I guess I still think of CSS as having selectors and JS/JQ as just borrowing the syntax. If they'd asked about selector methods, it would be a lot more clear. Mar 5 '13 at 15:33
  • The API is called the selectors API. That's clear to me. Mar 5 '13 at 15:35
  • Still a really stupid interview question, IMO. Mar 5 '13 at 15:40
  • On that I may agree. Many good developers could very well miss what was meant by the question. Mar 5 '13 at 15:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.