JavaScript conventions - variable declaration

What is the less error prone approach to declaring variables in JavaScript?

var a; var b; var c; 

or

 var a, b, c; 

jQuery and Dojo use the second approach, and personally, that my favorite in terms of code clarity, the problem is that it's harder for me to debug.

Example 1:

 var a, b, c; // oops.. semicolon instead of comma d, e; 

Example 2:

When searching for a specific variable in a var project, it gets better than the variable is declared.

EDIT . The initial examples were wrong, I made a mistake while copying and pasting. The code has already been updated, sorry for the inconvenience.

+6
source share
5 answers

A few variable declarations (rather than combined ones) are less error prone for the reasons you talked about. However, consolidated declarations have some other advantages:

  • JSLint and possibly other linters will complain about several var keywords for each scope.
  • The only var keyword, for obvious reasons, displays better than many others.
  • Combining declarations forces you to declare variables in one place, probably close to the beginning of the function in which they are located, which is considered good practice.

On the flip side, as you mentioned, errors can be made with combined variable declarations, and they can be awkwardly presented in diff.

As long as you maintain a consistent style for declaring variables throughout the project, the style you choose does not really matter.

+5
source

I prefer your second approach to use only one var keyword. Recently, I took another step and created type instances for each variable in order to prevent unwanted type changes later. Example:

 var a = 0, b = 0, c = {}, d = [], f = ""; 

I will put the actual value here if I have a value for delivery at this moment. If not, I will put the following dummy values:

  • 0 for room types
  • [] for the array
  • {} for object literals and DOM nodes
  • "" for string
  • function () {return;} for functions
  • false for boolean
  • For this purpose, I'm not worried about assigning types to Math, Date, or RegExp types.
+2
source
 var a; var b; var c; 

is the best approach because it will not throw an undefined error. Also, the variables will be correctly defined, and not in the global scope.

 var a=10; b=5; 

will result in b in the global area.

EDIT: This was in context with the sample originally published https://stackoverflow.com/revisions/9746359/2 . With an updated Context, it comes down to more individual preferences. My recommendation would be to not give room for such errors, as they are difficult to find.

+1
source

The second option is better in my book, since it is less redundant. Modern browsers and IDEs are pretty easy to find the error as you caused it.

Here's a jsFiddle that shows what is happening. Accidentally including a semicolon in a series raises an Uncaught ReferenceError . This is a quick fix and a little compromise for a stronger syntax. If this is the worst mistake you have to deal with when writing JavaScript, you have a great day.

Ironically, in the second example, you had a typo - a semicolon after a . Oops. :)

EDIT: Original poster with a fixed typo; I am leaving a mention because this is relevant to the original question.

0
source

There is one bonus to multiple variable declarations using 1 var.

You will have a lower script score. Obviously, the 4 bytes needed to declare var do not really matter to your place, such as stackoverflow.com. Millions upon millions of downloads == Gigabytes of transfer based only on a few additional ads var .

0
source

Source: https://habr.com/ru/post/910963/


All Articles