I'm adding ESLint to my Node project and cannot figure out how to change this code to work properly:
const connection = {};
for (let [prop, value] of connectionString) {
prop = prop.split(' ')[0];
connection[prop] = value;
}
I'm getting error:
'value' is never reassigned. Use 'const' instead.
I'm adding ESLint to my Node project and cannot figure out how to change this code to work properly:
const connection = {};
for (let [prop, value] of connectionString) {
prop = prop.split(' ')[0];
connection[prop] = value;
}
I'm getting error:
'value' is never reassigned. Use 'const' instead.
1 Answer
Reset to default 5Rather than reassigning prop
, create a new variable for the first word. That way, both prop
and value
can be declared with const
:
const connection = {};
for (const [prop, value] of connectionString) {
const firstWord = prop.split(' ')[0];
connection[firstWord] = value;
}
Most of the time, clean readable code can work just fine without ever reassigning a variable. Best to only reassign an existing variable when you absolutely have to - that's a big part of why the rule exists, to prod you to use const
(and produce more readable code as a result).
You could also achieve it without the intermediate variable:
const connection = {};
for (const [prop, value] of connectionString) {
connection[prop.split(' ')[0]] = value;
}