最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is ESLint throwing warning about unused variable that's used? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Oct 20, 2018 at 5:42 bot19 asked Oct 20, 2018 at 5:12 bot19bot19 81410 silver badges19 bronze badges 12
  • 3 why not if (condition) console.log('hello world')? – Patrick Roberts Commented Oct 20, 2018 at 5:15
  • 2 Your code is fine. It logs null or hello world. Whats the problem? – Teemoh Commented Oct 20, 2018 at 5:17
  • 3 @bot19 can you help us to understand what the problem is? From what we can tell, this code would be no different than if that variable were to be declared with var. What are the circumstances that you're not telling us? – Patrick Roberts Commented Oct 20, 2018 at 5:20
  • 3 So, this is now an ESLint question? – jfriend00 Commented Oct 20, 2018 at 5:28
  • 2 @bot19 the key phrase in your question is "before I referenced the variable in the console log". ESLint is recognizing that you performed some logic and stored the result in a variable that wasn't being used, so it's pointing out that you either need to return it or not use a variable at all. – Patrick Roberts Commented Oct 20, 2018 at 5:29
 |  Show 7 more ments

2 Answers 2

Reset to default 3

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/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

发布评论

评论列表(0)

  1. 暂无评论