Swap a word in the DOM from an array of words I have question about my code. It should swap words from an array into an HTML Element and it does so like expected. However, I was wondering about the style of my code. I would like to get some feedback on how to improve the code.

Javascript

const words = [
    "Webdesigner",
    "Frontend Entwickler",
    "Webentwickler",
    "eine Flitzpiepe"
];

const interval = 2000;

function swapWords(words, interval){

    const swappingWord = document.getElementById("swappingWord");
    let i = 0;

    setInterval(function(){

        swappingWord.textContent = words[i].toUpperCase();
        i++;

        if(i == words.length){
            i = 0;
        }
    }, interval)
}
swapWords(words, interval)


HTML

&lt;span id="swappingWord"&gt;WEBDESIGNER&lt;/span&gt;
There is no way to remove the behavior. This is usually bad design.
Since there is no way to remove behavior, swapWords() as a function is unneeded: you will never call it twice. It would only make sense defining it as a function if it could be called on different elements.
The initial state of the cycle is undefined and/or inconsistent: since the first swap only occurs after 2s, until then the initial content of the span will be displayed (WEBDESIGNER). Then it will be swapped with... WEBDESIGNER. Either leave the span empty and explicitly call a swap round on initialisation, or push the initial contents of the span on top of the stack of words. Otherwise it's not a cycle, it's a "weird start, then a cycle".
if(i == words.length){ - ok, you defined words as const so this is safe. But it's a bad habit to develop to write code like this: if words was variable between rounds, as it got trimmed from, say, 6 items to 4, and i would be 5, the condition would not be triggered anymore, and also not checked before access. So it would just go on to infinity. The responsible way to write code is always update and bound-check any state vars in a synchronous matter, which means BEFORE using them (i.e. before swappingWord.textContent = words[i].toUpperCase();), not at the end of the round for the next async round. Also always use &gt;=words.length not ==words.length, as a coding habit. Always test for valid/invalid condition, not edge condition. In case you decide later to make words list variable, the == condition will automatically introduce a bug. &gt;= won't. The remarks of this point don't qualify as bad code (it works as long as you don't change initial assumptions), it's just bad style. Get rid of it before you get used to it :)