Multiple assignment in javascript? What does [a,b,c] = [1, 2, 3]; mean?
This is a feature called destructuring assignment, which was added in JavaScript 1.7 and ECMAScript 6. It is not a part of ECMAScript 5: What is cross browser support for JavaScript 1.7's new features? Specifically array comprehensions and the "let" statement
How this type of initialization of variable in JS is called
This is called destructuring assignment in JavaScript.
The destructuring
assignment
syntax is a JavaScript expression that makes it possible to unpack
values from arrays, or properties from objects, into distinct
variables.
In your example let [a,b,c] = arr;
, you are unpacking the source object arr
, and assigning them to 3 variables a
, b
and c
.
We already saw one example of destructuring assignment on an array above.
The general form of the syntax is:
[ variable1, variable2, ..., variableN ] = array;
This will just assign variable1
through variableN
to the corresponding item in the array
. If you want to declare your variables at the same time, you can add a var
, let
, or const
in front of the assignment:
var [ variable1, variable2, ..., variableN ] = array;
let [ variable1, variable2, ..., variableN ] = array;
const [ variable1, variable2, ..., variableN ] = array;
Multiple left-hand assignment with JavaScript
Actually,
var var1 = 1, var2 = 1, var3 = 1;
is not equivalent to:
var var1 = var2 = var3 = 1;
The difference is in scoping:
function good() { var var1 = 1, var2 = 1, var3 = 1;}
function bad() { var var1 = var2 = var3 = 1;}
good();console.log(window.var2); // undefined
bad();console.log(window.var2); // 1. Aggh!
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)
Meaning or functionality of this const variable
It's called array destructuring assignment
in JS. It unpack values
from arrays, or properties from objects, into individual variables
.
const [num1, num2] = [11, 22, 33];
console.log(num1, num2);
Is there a way to do multiple assignment and initialization with block scope?
You can do it in a single let
declaration as follows:
{
let a = "wang", b = a;
}
console.log("b:", b); //undefined or ReferenceError
Because both a
and b
are declared with let
inside the braces, they both get block scope. You have to assign a
first so that it's been assigned before you assign it to b
also.
Unfamiliar use of square brackets during multiple variable declaration in Javascript
But what goes on under the hood?
// You declare a string variable
var testString = "firstName, lastName";
// Split method help you to divide the string value according with the
//indicated separator, in this examle the comma
var [a,b] = testString.split(", ");
The destructuring assignment syntax is a JavaScript expression that
makes it possible to unpack values from arrays, or properties from
objects, into distinct variables.
Since the split function returns an array, with the var [a,b] = array
you are assigning the value in index order, in the example:
console.log(a); // 'firstName'
console.log(b); // 'lastName'
And they are simple string variables. You may want to vist the links below:
Destructuring asignation
split function
Further resources: Since you have mentioned you are beginning with JS, I suggest you to read books mentioned in this magnific post
What is going on with this assignment?
It's a destructuring assignment on the results of calling the useState
function:
- the first element of the array is the value
- the second element is a function to change value.
Related Topics
Stop Execution of JavaScript Function (Client Side) or Tweak It
Differencebetween Parseint() and Number()
Is the Order of Elements in a JSON List Preserved
Is Safari on iOS 6 Caching $.Ajax Results
Nodejs VS Node on Ubuntu 12.04
Accessing Jpeg Exif Rotation Data in JavaScript on the Client Side
The Value of "This" Within the Handler Using Addeventlistener
Get File Size Before Uploading
Dealing with Float Precision in JavaScript
Calling an Asynchronous Function Within a for Loop in JavaScript
Browser JavaScript Stack Size Limit
Are JavaScript Arrays Primitives? Strings? Objects
Why and When Do We Need to Bind Functions and Eventhandlers in React
How to Read the Client's MAChine/Computer Name from the Browser
How to Get Node Datum on Mouseover in D3 V6
Do Common JavaScript Implementations Use String Interning
Why Doesn't My Equality Comparison Using = (A Single Equals) Work Correctly