element.classList
<style>
    .bold {
        font-weight: 600;
    }
    .bg-red {
        background-color: red;
    }
</style>
<p class="bold">Clicking the button adds the "bg-red" class value.</p>
<button type="button">Add a Class Value to the Element</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");

button.addEventListener("click", () => { // Button click event
    // Add the "bg-red" class value
    elem.classList.add("bg-red");
});
Rendered Output Click the "Add a Class Value to an Element" button!
<style>
    .bold {
        font-weight: 600;
    }
    .bg-red {
        background-color: red;
    }
</style>
<p class="bold bg-red">Clicking the button removes the "bg-red" class value.</p>
<button type="button">Remove a Class Value from the Element</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");

button.addEventListener("click", () => { // Button click event
    // Remove the "bg-red" class value
    elem.classList.remove("bg-red");
});
Rendered Output Click the "Remove a Class Value from the Element" button!
<style>
    .bold {
        font-weight: 600;
    }
    .bg-red {
        background-color: red;
    }
</style>
<p class="bold">The background color is added or removed.</p>
<button type="button">Add/Remove Background Color</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");

button.addEventListener("click", () => { // Button click event
    // Adds the "bg-red" class value if it does not exist; otherwise removes it
    elem.classList.toggle("bg-red");
});
Rendered Output Click the "Add/Remove Background Color" button!
<style>
    .bold {
        font-weight: 600;
        color: white;
    }
    .bg-red {
        background-color: red;
    }
    .bg-blue {
        background-color: blue;
    }
</style>
<p class="bold bg-red">The background color changes when the button is clicked.</p>
<hr>
<button type="button">Replace Class Value</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");

button.addEventListener("click", () => { // Button click event
    // Replaces the "bg-red" class value with "bg-blue" if it exists
    elem.classList.replace("bg-red", "bg-blue");
});
Rendered Output Click the "Replace Class Value" button!
<video src="video.mp4" class="a b c"></video>
const myVideo = document.querySelector("video");

console.log(myVideo.classList.contains("a")); // true
console.log(myVideo.classList.contains("b")); // true
console.log(myVideo.classList.contains("d")); // false
<div class="one two three">This is a div element.</div>
const elem = document.querySelector("div");
const classCount = elem.classList.length;

console.log("Number of class values: " + classCount);
// "Number of class values: 3"
<div class="one two three">This is a div element.</div>
const elem = document.querySelector("div");
const classValue = elem.classList.value;

console.log(classValue);
// "one two three"