14 May 2023

Both the native JavaScript and jQuery methods achieve the same result of checking whether an element has a particular class. However, the syntax and implementation are different between the two. In general, using the native JavaScript method may be more efficient since it does not require loading the entire jQuery library. However, if you are already using jQuery in your project, using the hasClass() method can make your code more concise and easier to read.

Source code viewer
  1. const hasClass = element.classList.contains('my-class');
Programming Language: Javascript