How to clear a variable value in JavaScript?

Member

by aubrey , in category: Javascript , a year ago

How to clear a variable value in JavaScript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , a year ago

@aubrey 

In JavaScript, you can clear a variable value by assigning it a new value of undefined. Here's an example:

1
2
3
4
5
6
let myVariable = 10;

// Clear the value of the variable
myVariable = undefined;

console.log(myVariable); // Output: undefined


Alternatively, you can also use the null value to clear a variable, like this:

1
2
3
4
5
6
let myVariable = 10;

// Clear the value of the variable
myVariable = null;

console.log(myVariable); // Output: null


Both methods will effectively clear the value of the variable.