Should I use "hasClass" before "addClass"?

You can see at the source code : https://github.com/jquery/jquery/blob/master/src/attributes/classes.js#L38-L45 that they do check if the class exists when using addClass.

So there is no reason to use the .hasClass() in this case.. (an exception would be if you wanted to perform more actions if the element did not have the class..)


The .hasClass() check is not useful because jQuery will also always check from within .addClass(). It's just extra work.