How to implement a function that returns 0 or 1 each time it is executed

how to implement a function that returns 0 or 1

each time it is executed

beginner js, wants to implement a toggel function similar to jQuery

related codes

// :
fn(){}
fn() //0  1
//fn()
Dec.03,2021

var fn = (function(){
    var bool = true;
    return function(){
        bool = !bool;
        console.log(bool ? 1 : 0);
    }
})()
fn()
fn()

// flag=truefalse 
function toggle(flag){
    return !flag
}

the first reaction is also closure

Menu