cableakp.blogg.se

Javascript includes array
Javascript includes array













javascript includes array javascript includes array

Maps (and WeakMaps) are relatively new features and act as a sort of “better” object (I won’t go into the differences in this article). classList: contains()ĬlassList is a DOMTokenList, and those use contains(). You call a function on the list with the item as an argument and you end up with a boolean that tells you if the item is in the list or not: // returns true or falseįor arrays, you need to use the includes() function. The function name to use for this depends on the type of list you’re working with, even though the signature of each function is exactly the same. For example, I do it when I need to check if a specific class is applied to an element, or if a return value of a function matches one of multiple values. The main problemĬhecking if a given thing is in a list is a common action in JavaScript (for me at least). So I’m writing this for my own reference. You see, there are three possible options: includes(), contains() and has() and I mix them up all the time. Every single time I need to look up the function that does that for the sort of list I’m working with. When writing JavaScript, very frequently I need to check if a given thing exists in a list of things.















Javascript includes array