Unset a JavaScript Variable



To unset a variable in JavaScript, use the undefined. After that, use delete operator to completely remove it.

Let’s try the code snippet again.

var str = “Demo Text”;
window.onscroll = function() {
   var y = this.pageYOffset;
   if(y > 200) {
      document.write(nxt);
      nxt = undefined; // unset
     delete(nxt); // this removes the variable completely
     document.write(nxt); // the result will be undefined
   }
}
Updated on: 2019-09-13T07:44:06+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements