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?
1
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.
15
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.
6