Declaring multiple variables in JavaScript
The first way is easier to maintain. Each declaration is a single statement on a single line, so you can easily add, remove, and reorder the declarations.
With the second way, it is annoying to remove the first or last declaration because they start from the var
keyword and finish with the semicolon respectively. Every time you add a new declaration, you have to replace the semicolon in the last old line with a comma.
Shorter way to declare multiple variables in JavaScript?
"Faster"? You've determined there's a performance bottleneck here?!
In any case, you can declare multiple variables:
let foo = 'foo'
, bar = 'bar'
, foobar = 'foobar'
;
But this is simply JS syntax–is this what you are really asking?
If you have a "large" number of related variables the problem may be more systemic, and there are multiple types of refactorings that might help.
Updated: I used to declare variables like this; I don't anymore.
Declare multiple variables in JavaScript
Yes, it is if you want them all to point to the same object in memory, but most likely you want them to be individual arrays so that if one mutates, the others are not affected.
If you do not want them all to point to the same object, do
var one = [], two = [];
The []
is a shorthand literal for creating an array.
Here's a console log which indicates the difference:
>> one = two = [];
[]
>> one.push(1)
1
>> one
[1]
>> two
[1]
>> one = [], two = [];
[]
>> one.push(1)
1
>> one
[1]
>> two
[]
In the first portion, I defined one
and two
to point to the same object/array in memory. If I use the .push
method it pushes 1 to the array, and so both one
and two
have 1
inside. In the second since I defined unique arrays per variable so when I pushed to one, two was unaffected.
javascript multiple variable assignment in one line
You can write
var start = Date.now(),
diff,
minutes,
seconds;
as
var start = Date.now();
var diff;
var minutes;
var seconds;
So you can declare multiple variables in one line as in code snippet 1 where we are initialising start
but only declaring other variables.
You can initialise multiple in one line like
var a = 1, b = 2, c = 3;
declaring multiple variables in a single statement
The comma, in this situation, allows you to declare multiple variables without having to re-use the var
keyword over and over again:
var variable1 = 'foo',
variable2 = 'bar';
declaring multiple variables in javascript based on the quantity of children
Use a loop to add the mutation observer to all the DIVs.
document.querySelector(".text.right").forEach(span => {
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var spantext = span.textContent;
var spandiv = span.parentNode;
if (mutation.addedNodes) {
if (spantext > avg) {
spandiv.style.backgroundColor = "#E8E8E8"
spandiv.style.color = "black";
spandiv.style.opacity = "0.7";
}
if (spantext < avg) {
spandiv.style.backgroundColor = "black";
spandiv.style.color = "white";
spandiv.style.opacity = "1";
}
}
})
});
const options = {
childList: true,
subtree: true,
attributes: true,
characterData: true
};
observer.observe(span, options);
});
Assign multiple variables to the same value in Javascript?
Nothing stops you from doing
moveUp = moveDown = moveLeft = moveRight = mouseDown = touchDown = false;
Check this example
var a, b, c;a = b = c = 10;console.log(a + b + c)
Related Topics
What Is Meant by 'First Class Object'
How to Sort 2 Dimensional Array by Column Value
How to Display Length of Filtered Ng-Repeat Data
How to Get the Size of a JavaScript Object
Why Does Google Prepend While(1); to Their JSON Responses
Differencebetween Parseint() and Number()
How to Get the Scrollbar Position with JavaScript
Check/Uncheck Checkbox with JavaScript
Comparing Date Part Only Without Comparing Time in JavaScript
Interface Type Check with Typescript
Asynchronously Load Images with Jquery
How to Listen to a "Style Change" Event
Browsers, Time Zones, Chrome 67 Error (Historic Timezone Changes)
Node.Js on Multi-Core MAChines
Using "Object.Create" Instead of "New"