Question about string rotation

So i found this exercise on W3Sources about string rotation, i checked out the solution because i wasnt sure what they meant about rotating it. Now i read trought the code and did code along, i understand the way it works thanks to console.log, but one thing i dont get is. Why do we need to use in interval textNode .data = text? Now i understand that gets data from element.childNodes[0] in already defined var of textNode that stores is but why doesnt it work if we just use text = text[text.length - 1] + text.substring(0, text.length - 1); alone in interval function? Why is it required to use = text? When we already have in var text?

<body onload="animate_string('target')">
<pre id="target">W3Sources</pre>


function animate_string(id) {
    var element = document.getElementById(id);
    var textNode = element.childNodes[0];
    var text =;

    setInterval(function () {
        text = text[text.length - 1] + text.substring(0, text.length - 1); = text;
    }, 1000)

Because you need to apply the changes to the node. If you don’t do = text you’re only assigning the modified string to the text variable, which is never returned.

If this helps making the logic clear, you can also do: = text[text.length - 1] + text.substring(0, text.length - 1);
1 Like