How to check if element has class in JavaScript

DOM
By Jad Joubran · 
Last updated Feb 26, 2024
<div id="box" class="active"></div>
const element = document.querySelector("#box");

// Check if hasclass
// Result is a boolean
element.classList.contains("active");
true
JavaScript has class on Learn JavaScript Blog