How do you "combine variables" in JavaScript to satisfy JSLint?

For a school project, I am trying to create a website that does things. For this I use HTML, JavaScript and CSS. I use a compiler that gives debugging hints. These tips are provided by JSLint . I was told that I should combine the two variables that I wrote, but I do not understand what this means.

I did some research (on Qaru and other websites), but they all come with people wanting to put variables into arrays.

Will someone kindly explain to me what should I do?

Code (simplified):

var x = document.getElementById("some id"); var y = document.getElementById("some other id"); var z = document.getElementsByTagName("some tag name"); 

And JSLint says:

"Combine this with the previous var statement: var y = document.getElementById (" some other identifier "); and

Combine this with the previous "var" statement. var z = document.getElementsByTagName ("some tag name");

Important note: the code works , but JSLint tells me about it.

+7
javascript jslint
source share
3 answers

Just so you know what you wrote, this is valid JavaScript. The error is related to style. This is important if you are in strict mode.

What the error says, you need to combine all the variable declarations into one line, using the comma operator to separate them.

 var x = document.getElementById("some id"), y = document.getElementById("some other id"), z = document.getElementsByTagName("some tag name"); 

You can get explanations about JSLInt errors here

+6
source share
 var x = document.getElementById("some id"), y = document.getElementById("some other id"), z = document.getElementsByTagName("some tag name"); 

You can declare variables, as in the example above.

+9
source share

This tells you that you are linking variable declarations and not publishing them separately. In js, you do this with a comma to separate the declarations, not a colon to indicate the end of the statement.

Listed below are 3 separate decartations ...

 var x = document.getElementById("some id"); var y = document.getElementById("some other id"); var z = document.getElementsByTagName("some tag name"); 

To make this one declaration of three different variables (with their definitions) ...

 var x = document.getElementById("some id"), y = document.getElementById("some other id"), z = document.getElementsByTagName("some tag name"); 

NOTE This still needs to end with a semicolon to delimit the statement (variable declaration).

+9
source share

All Articles