let flag = false; const toggle = () => { if ( flag ) { // insert code here when flag is on/true } else { // insert code here when flag is off/false } flag = ! flag; }