What Does Variable Declaration with Multiple Comma Separated Values Mean (E.G. Var a = B,C,D;)

What means this jquery code with two values in a variable?

You're declaring two variables and the first one has a value.

var target = $(e.target), article;

is the same as:

var target = $(e.target);
var article;

This not jQuery related -- rather "vanilla" Javascript.

Helpful material: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Syntax of variable in javascript

No this is not the same. This would be the same:

var option_1 = 0, option_2 = 0;

In your case #2 has option_1 set to 0, and option_2 being undefined.

What is the difference between a single comma-separated variable declaration, and multiple declarations?

The comma separated declaration is just a short hand. Executing-wise there's no difference. But it can help reduce the size of your javascript file if you are declaring a lot of variables.

You can even do:

var a = 1, b = 'string', c = new Date();

Javascript variable definition - clarification

It's creating 3 variables

var line = {};   // creates an object
var lines = []; // creates an array
var hasmore; // undefined

Elements and functions separated by commas

It's just a bunch of variable declarations/initializations, separated by commas. It's the same as:

var _t = this;
var _resetProperties;
var _add_html5_events;
var _remove_html5_events;
var _stop_html5_timer;
var _start_html5_timer;
var _attachOnPosition;
var _onplay_called = false;
var _onPositionItems = [];
var _onPositionFired = 0;
var _detachOnPosition;
var _applyFromTo;
var _lastURL = null;
var _lastHTML5State;

I don't like to mix initializations with declarations like this. It is messy and not very readable. If anything, group your declarations (without values), and only group initializations if they are related. It doesn't functionally change the code, it only makes it a bit smaller (and harder to read in cases).

Code inside javascript function separated with commas instead of semicolons

In javascript, when declaring new variables with the var keyword, you can separate each declaration by commas. Which allows you to only use the var keyword once. Its a sort of short-hand.

For example:

var self = this,
options = self.options,
title = options.title || "No Title",
uiChatbox = (self.uiChatbox = $('<div></div>')).appendTo(document.body)

Is the same as:

var self = this;
var options = self.options;
var title = options.title || "No Title";
var uiChatbox = (self.uiChatbox = $('<div></div>')).appendTo(document.body);


Related Topics



Leave a reply



Submit