-6

Declaration of var in javascript shoud looks like this

var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];

valueN - Initial value of the variable. It can be any legal expression.

Can you provide me an exemple of that legal expression?

Karolina Ticha
  • 531
  • 4
  • 19

2 Answers2

1

Can you provide me an exemple of that legal expression?

As mention in the MDN

Expressions

An expression is any valid unit of code that resolves to a value.

Conceptually, there are two types of expressions: those that assign a value to a variable and those that simply have a value.

var a = 10;

var a = 20+24; 

this is also a expression

var age = 3;
var str = ["I'm only", age, "years old"].join(" ");
Gupta
  • 8,882
  • 4
  • 49
  • 59
  • @Karolina Ticha: could you tell us whether our answer has been work for you or still have some issue ? – Gupta Apr 03 '16 at 12:35
1

generally [] means optional. What your statement here means is that you can define any number of variables using a single var, separated by commas and you can assign(initialize) those variables at the same time.

Take this

var varname1 [= value1];

means varname1 can be initialized while defining otherwise

var varname1;

or

var varname1 = value1;

like wise

var varname1 [= value1 [, varname2 [= value2]]];

can be

var varname1 = value1, varname2 = value2;
var varname1 = value1, varname2;
var varname1, varname2 = value2;
var varname1 = value1;
var varname1;
etc

for ex:

var a = 1, b = 2, c, d, e = 5;
Venugopal
  • 1,888
  • 1
  • 17
  • 30