This is what I tried so far.
var nxt = 'I am next';
window.onscroll = function(){
var scr = this.pageYOffset;
if(scr > 400){
console.log(nxt);
delete(nxt); // unset here
console.log(nxt); // expected undefined or null
}
}
I used delete() expecting to remove the reference value inside the variable. Unluckily, it just remain the same. Can somebody give me a clue on how to achieve this kind of scenario?
Any help would be appreciated.
deleteoperator in its website. Variable is unable to usedeleteoperator to remove it. Assign it withundefinedinstead.deletetounset, but your variable should not declare asvarExample: window.onscroll = function(){ scr = this.pageYOffset; if(scr > 400){ console.log(nxt); delete nxt; // unset here console.log(nxt); // expected undefined or null } } It will work as expected. or you can set toundefinedornullnxt = undefined; (or) nxt = null;