I've been writing React & ES6 code for about 2 months now. Not sure if I just haven't ran into this problem, or if I'm having a brain freeze this morning.
Did some research and ran into a bunch of articles on const
& let
but nothing address this. I've been reading that you don't need to use var
anymore, so then how would you handle this situation?
function () {
let variable = null;
if (condition) {
variable = 'hello world';
}
console.log(variable); // want 'hello world' if condition
}
Edit: please assume function is being called & condition is true
.
I see the code works under these conditions but my confusion is this: before I referenced the variable in the console log, my ESlint reports [eslint]: 'variable' is assigned a value but never used
.
Isn't it being used?
By "using it", ESLint is referring to actually putting it to some use. Simply putting a value in it does not constitute using it, as far as ESLint is concerned.
A variable whose value is never accessed serves no purpose.
so its an eslint rule: https://eslint.org/docs/rules/no-unused-vars
if you are not familiar with what does eslint do:
Its goal is to provide a pluggable linting utility for JavaScript
if you think you wanna remove this rule you can add this line to .eslintrc
nikko:cms-v3 nikko$ cat .eslintrc.json
{
"rules": {
...
"no-unused-vars": "off", // add this
Isn't it being used?
it doesnt matter if function is called or not, if eslint sees you define a variable in that scope but you never used the variable, it will spit that error. What you did was re-defining the variable not using it.
let variable
needs to be somehow consumed by some process
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.