classList - manipulating element’s classes with pure JavaScript
In the past, if we wanna manipulate an element’s classes, we have to use class attribute and className property.
Because of they’re strings, which means less efficiency and performance.
Although we have third-part libraries to work around it, e.g. jQuery.hasClass or jQuery.addClass, but it’s still less efficiency.
But now, element.classList comes:

This property provides the following methods:
- add
- contains
- item
- remove
- toggle
Notice that all of them are only accept one valid class name in one invocation, otherwise you will get “INVALID_CHARACTER_ERR” exception.
Notes:
- if class is already exist,
addmethod will ignore it classListis similar witharguments: you can access one class byclassList[0], but can’t use array methods directly, e.g.classList.forEach(fn);
All the modern browsers are support it, but not IE9 or below. Yes, IE8- must die.