Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why single var is good in javascript?

Tags:

javascript

Can anyone tell me why use one var declaration for multiple variables and declare each variable on a newline consider is a good programming behavior?

// bad
var items = getItems();
var goSportsTeam = true;
var dragonball = 'z';

// good
var items = getItems(),
    goSportsTeam = true,
    dragonball = 'z';
like image 989
ldsenow Avatar asked Apr 03 '13 03:04

ldsenow


People also ask

Is it good to use VAR in JavaScript?

This means that if a variable is defined in a loop or in an if statement it can be accessed outside the block and accidentally redefined leading to a buggy program. As a general rule, you should avoid using the var keyword.

Why do we use VAR in JavaScript?

The var keyword is used to declare variables in JavaScript. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows. Storing a value in a variable is called variable initialization.

Why let is better than VAR in JavaScript?

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

Do people still use VAR in JavaScript?

For you question directly, var is probably still used for legacy reasons. It is supported in all versions of JavaScript and it would be a bother to change every example on the internet. The only real advantage to var is it's compatibility. If you are writing for old platforms like .


2 Answers

It is not considered 'good' or 'bad'. It's a matter of preference.

The guy who built the code quality tool JSLint, Douglas Crockford likes it.

One 'advantage' it might have is that it avoids the possibility of variable hoisting. In JavaScript all var declarations move to the top of their scope automatically.

Here is why Crockford thinks the second option is better:

In languages with block scope, it is usually recommended that variables be declared at the site of first use. But because JavaScript does not have block scope, it is wiser to declare all of a function's variables at the top of the function. It is recommended that a single var statement be used per function. This can be declined with the vars option.

like image 143
Benjamin Gruenbaum Avatar answered Oct 27 '22 00:10

Benjamin Gruenbaum


It's a preference, I wouldn't say good or bad. Yes JSLint complains about it, I don't really like how it complains about for loop variables being inline as well. The reason that it was put in JSLint was to prevent possible hoisting confusions.

Also in some cases declaring all of your variables at the top will lead to a slightly smaller file. Consider the following:

var a = 10;
a++;
var b = 20;

After Google Closure being run over it

var a=10;a++;var b=20;

As opposed to this if we pull b's declaration to the top.

var a=10,b;a++;b=20;
like image 30
Daniel Imms Avatar answered Oct 27 '22 00:10

Daniel Imms