Search code examples
javascriptarraysvariablesglobal-variablesreset

Reset all Variables to the same value - Javascript


I was looking around and i found that a few people had issues with how to reset all their variables to a set value.

So when i googled this i found a lot of tips that was almost doing it, but with a lot of unnecessary steps, and very much not beginnerfriendly readability in their scripts.


So im going to put down a few ways i figured might be the easiest way of doing this. thats also very easy for a beginner to read at a later time.

we are using these variables, and want to change them all to the same value.

var a = 1;
var b = 4;
var c = 2;
var d = 6;

Solution

  • below is a few ways you can kind of do this.


    With a function.

    var a = 1;
    var b = 4;
    var c = 2;
    var d = 6;
    
    function clearData(){
        a=b=c=d= false; //change false to what ever u want
    }
    

    With an array

    var a = 1;
    var b = 4;
    var c = 2;
    var d = 6;
    
    
    var dataSet = [
        a,b,c,d
    ] = Array(5).fill(0) //change the 0 to what ever u want
    

    This way i know i myself would find the code a lot easier to read aswell at a later time.


    Hope it can help you! :)