0

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;
Elly
  • 51
  • 8
  • `var` is deprecated. And global variables have their own problems, and hence there's almost always a better way to solve a problem than a global variable. – Andreas Aug 25 '22 at 09:57
  • 1
    [How do I ask and self-answer a correct, high quality Q&A pair without attracting downvotes?](https://meta.stackoverflow.com/questions/314165/how-do-i-ask-and-self-answer-a-correct-high-quality-qa-pair-without-attracting) - If you're going to answer your own question you should still write it as an actual question. – Andreas Aug 25 '22 at 09:58
  • @Andreas it's not actually deprecated – GrafiCode Aug 25 '22 at 10:01
  • also, everything here is already on stackoverflow – GrafiCode Aug 25 '22 at 10:03
  • Does this answer your question? [Assign multiple variables to the same value in Javascript?](https://stackoverflow.com/questions/16975350/assign-multiple-variables-to-the-same-value-in-javascript) – GrafiCode Aug 25 '22 at 10:03
  • 2
    @GrafiCode Not actually _deprecated_ but it's the legacy-way of doing variables. _Imho_: You really should skip `var` completely and go with `let` and `const` ¯\\(°_o)/¯ – Andreas Aug 25 '22 at 10:08
  • @Andreas yes I agree – GrafiCode Aug 25 '22 at 10:10

1 Answers1

0

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! :)

Elly
  • 51
  • 8