Two solutions, which is best practice

Hey everyone, I have a simple counter element that can be increased and decreased, this is the way I chose to do it.
let numberOfItems = document.querySelector('.number-of-items')
let decreaseItem = document.querySelector('.decrease-item');
let increaseItem = document.querySelector('.increase-item');

decreaseItem.addEventListener('click', subtractItem)
increaseItem.addEventListener('click', addItem)

let counter = 0;

function addItem(){
    counter += 1;
    numberOfItems.innerHTML = counter;
}

function subtractItem(){
    if (counter === 0){
        alert("You don't have this item in your cart.")
    }
    else {
        counter -= 1;
        numberOfItems.innerHTML = counter;
    }
}
Was this page helpful?