Why does this work:
let count = 0
const addOne = () => count += 1
And this does not:
let count = 0
const addOne = (count) => count += 1
Why does this work:
let count = 0
const addOne = () => count += 1
And this does not:
let count = 0
const addOne = (count) => count += 1
Hello~!
You are using the same name for the function parameter as you are for the variable. So the function written as (count) => count += 1
is changing the value of the parameter count
and not the variable count
.
If I change the parameter name (and nothing else), the function performs as expected.
As a side note…
const addOne = (num) => count += 1
could be considered bad form - I’ve created a function that requires a parameter but doesn’t use that parameter.