1

I'm filtering my data and should I use let in storing the percentage or would it cause problems in the long run?

I plan to use this in the graph too.

const filteredUsers= data.filter((v) => v.items?.selectedItem == "Car");
const total = filteredUsers.length

const items2 = users.filter(
    (v) => v.items?.part1 == true && v.items?.part2 == true
  );

  let percentage = ((items2.length / total) * 100).toFixed(2);
JS3
  • 1,623
  • 3
  • 23
  • 52
  • 4
    If you don't have requirement of again changing the `percentage` variable in later execution then i recommend `const` or else `let` is good. – Ankur Sep 17 '21 at 06:23
  • Read this answer: difference btw const vs let vs var https://stackoverflow.com/a/11444416/8657746 and then decide according to your requirement. – Hamza Iftikhar Sep 17 '21 at 06:27
  • 1
    Does this answer your question? [What is the difference between 'let' and 'const' ECMAScript 2015 (ES6)?](https://stackoverflow.com/questions/22308071/what-is-the-difference-between-let-and-const-ecmascript-2015-es6) – JorgeB Sep 17 '21 at 06:29
  • Question: Variable `total` is comming from? It could be the sum of some numeric property in the filtered user's array? – Yosvel Quintero Sep 17 '21 at 06:41
  • @YosvelQuinteroArguelles the `total` comes from the length of the filtered array – JS3 Sep 17 '21 at 09:53

1 Answers1

1

Variables defined with let cannot be redeclared. With var you can.

ES6 introduced let and const. These two keywords provide Block Scope in JavaScript. Variables declared inside a { } block cannot be accessed from outside the block

Variables declared with the var keyword can NOT have block scope. Variables declared inside a { } block can be accessed from outside the block.

So the question is, can you use const or do you change the variable percentage somewhere again, and do you need the variable outside of your block.

Legit007
  • 181
  • 1
  • 8