Reputation: 556
I want to ask a question about JavaScript variable declaration.
I am learning about node.js and I just started declaring the packages that I need for an application:
var bodyParser = require("body-parser"),
mongoose = require("mongoose"),
express = require("express"),
app = express();
but I was wondering something about the lines above.
When I was learning programming languages first, the elementary lesson was about variables and their declaration:
var myString = "Hello World"
var myNumber = 5
where each variable is defined separately on a new line. But, in the block of code that I've been presented with, I was struggling to understand what was happening.
My idea is that we are declaring out individual variables without the repetition of the var
keyword:
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var express = require("express");
var app = express();
This, I thought, would be the longer way around the matter. However, I failed to find any documentation to support my educated guess.
Am I declaring individual variables without repetition of the keyword var
or am I doing something else?
Upvotes: 1
Views: 5044
Reputation: 18909
You can declare the variables over a single line, multiple lines with a comma or even a combination of both:
var foo = 'foo', bar = 'bar',
baz = 'baz';
JavaScript doesn't require they are declared on a single line - they don't even need a semicolon to terminate the statement - as long as they are on separate lines:
var foo = 'foo'
var bar = 'bar'
var baz = 'baz'
Much comes down to a team's style, but for consistency and clarity, I recommend you declare each variable on a new line and terminated with a semicolon.
var foo = 'foo';
var bar = 'bar';
var baz = 'baz';
If variables are not changing, use const foo = 'foo;
, also consider using let declarations:
let allows you to declare variables that are limited to a scope of a block statement, or expression on which it is used, unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.
Upvotes: 2
Reputation: 2442
A: Defining multiple variables with one var
keyword is an extension of simple var x;
and well defined in the language specification.
From the ECMAScript Latest Draft
13.3.2 Variable Statement
Note
A var statement declares variables that are scoped to the running execution context's VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. Within the scope of any VariableEnvironment a common BindingIdentifier may appear in more than one VariableDeclaration but those declarations collectively define only one variable. A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer's AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.
Syntax
VariableStatement[Yield, Await]: var VariableDeclarationList[+In, ?Yield, ?Await]; VariableDeclarationList[In, Yield, Await]: VariableDeclaration[?In, ?Yield, ?Await] VariableDeclarationList[?In, ?Yield, ?Await], VariableDeclaration[?In, ?Yield, ?Await] VariableDeclaration[In, Yield, Await]: BindingIdentifier[?Yield, ?Await] Initializer[?In, ?Yield, ?Await] opt BindingPattern[?Yield, ?Await] Initializer[?In, ?Yield, ?Await]
Pay special attention to this part:
VariableDeclarationList[In, Yield, Await]: VariableDeclaration[?In, ?Yield, ?Await] VariableDeclarationList[?In, ?Yield, ?Await], VariableDeclaration[?In, ?Yield, ?Await]
Which means: "A VariableDeclarationList
is either a simple VariableDeclaration
(var myString;
) or a VariableDeclarationList
followed by a ,
and a VariableDeclaration
(var myString, myOtherString;
)". The fact that VariableDeclarationList
uses self references in its definition allows for recursion and for more than one variables being defined.
Possible duplicates:
Upvotes: 0
Reputation: 876
Yes You are doing it correct! however
if you miss comma it will not work as expected there. eg.
var a=10,
c = 20 d = 30
In above case if there is no comma or anything at end of line then javascript will automatically consider there is ; semi colon.
if that considered with ; eg: c = 20; that means end of statement so the var keyword used for a will not be applicable to d;
so if you using , then you can continue creating multiple variables using single var.
Also read about Hoisting.
Upvotes: 0
Reputation: 6724
There is no problem with your examples you can use them in any way as soon as your compiler can compile it.
Yes with basic es5 compiler, you don't need to repeat var
variable you can use commas to write or assign a new variable. So for the compiler, it is fine.
We usually have two main problems with them
Consistency in your code structure
Writing everything in the same way or with a style-guide helps developers to read and debug their code. That is why we use linting rules which help us to follow a style guide. In your structure, you can define to use var
for each line or not.
Compiled project size
It would be perfect to have smaller sizes for bundles and using less character would be awesome for it! BUT... There are already other tools for that. Webpack, babeljs, etc. When you have these kind of tools you don't have to think about your bundles and character sizes.
Upvotes: 0
Reputation: 2496
LINT wants a single var declaration statement, but it can be spread over multiple lines.
The reason it wants a single statement is to avoid any confusion about which variables belong to the local scope. With a single var statement, all locally scoped variables are contained to a single location within the scope and anyone can read the code quickly to see what they are.
It is also recommended that this declaration statement be at the top of the scope, since the JavaScript hoisting mechanism moves them there before execution anyway. By writing your code to expect that statement at the top of the scope, the hoisting mechanism can't cause any unexpected behavior.
Upvotes: 1
Reputation: 1951
You are correct. You just avoiding 'var' repetition. This link might be helpful. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
Upvotes: -1