I
I
Ivan2018-03-03 19:28:54
JavaScript
Ivan, 2018-03-03 19:28:54

Is JSlint right and why?

I decided to check my project with jslint (I hadn't used linters before).
Download jslint addin in VScode. All default settings.
I have this code (simplified):

if(true){
var newItem = 10;
}
else{
var  newItem = 20;
}

linter says to do this:
var newItem;
if(true){
newItem = 10;
}
else{
newItem = 20;
}

Please explain why the second option is considered correct?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-03-03
@Iv_and_S

Replace with let and see if your construct works.
In general, variables should be declared at the beginning of the function.
Also read about raising var. Two vars are simply meaningless.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question