How to make the clearInterval work and why isn’t it working?

I have a condition here which is if a = 6, stop the setInterval so I used clearInterval for my condition, but it doesn’t take effect, anybody can help me how I can make the clearInterval work under that condition?

function doSomething() {

        let a = 1;

        return setInterval(()=> {
            if (a < 6){
                 a++;
                console.log(a);
            }
            else {
                a = 1;
            }
        }, 1000)
    }

    setTimeout(doSomething, 5000);
    
    var id = doSomething();
    
    if (a = 6){
        clearInterval(id);
    }

Source: Ask Javascript Questions

LEAVE A COMMENT