This question already has an answer here:
Destructuring assignment in while loop in ES6 function doesn't propogate out of loop?
(1 answer)
Closed 6 years ago.
I am simply using es6 to reassign variables a & b. Why do I get an error if I leave the semicolon off of the a and b declaration and assignment statements? Does the parser try to pull a property out of 2 if the semicolon is left off? let b = 2[a, b]...?
Works:
let a = 1;
let b = 2;
[a, b] = [b, a]
Error:
let a = 1
let b = 2
[a, b] = [b, a]
I am looking for the actual reason this fails. Thanks!
Does the parser try to pull a property out of 2 if the semicolon is left off? let b = 2[a, b]...?
Yes. You cannot safely start a statement with a [ if you're not explicitly ending statements with semicolons.
This has been written about in many places, and it's one of the many things that standardJS (or other linters) will warn you about: http://standardjs.com/rules.html#semicolons
Related
This question already has answers here:
Destructuring assignment and variable swapping
(1 answer)
Semicolon before square bracket
(2 answers)
ES6 Array destructuring weirdness
(4 answers)
Closed 1 year ago.
Please, someone cound explain this behavior with and without semicolon?
Without semicolou, it raise a ReferenceError: Cannot access 'b' before initialization
let a = 1
let b = 2
[a,b]=[b,a]
It's ok with semicolon:
let a = 1;
let b = 2;
[a,b]=[b,a]
It doesn't occurs using var.
This line, without semicolons, evaluates to let b = 2[a,b..., and since b is being declared in the same expression, it throws the error. Adding a semicolon after the 2 will remove the attempted index and resolve the error.
This question already has answers here:
Array.prototype.fill() with object passes reference and not new instance
(7 answers)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Unexpected behavior using Array Map on an Array Initialized with Array Fill [duplicate]
(1 answer)
Closed 1 year ago.
To my knowledge, if we create multiple variables using Array().fill(), it will create unique variables without any reference. Below are two code blocks and output.
let [a, b] = Array(2).fill(false);
console.log(a, b); // Output: false false
a = true;
console.log(a, b); // Output: true false
let [x, y] = Array(2).fill([]);
console.log(x, y); // Output:[] []
x.push(1);
console.log(x, y); // Output:[1] [1]
The first code block is okay, no reference is there. But what happens in the second one. I'm expecting the output "[1] []". Please clarify once what is happening here.
This question already has answers here:
Simplest way to merge ES6 Maps/Sets?
(16 answers)
Closed 3 years ago.
Just spotted strange behavior of JS's Set object and decided to ask stackoverflow community :) So that's the problem:
Screen from console:
Code for copy-paste:
let a = new Set([1])
let b = new Set([2, 3])
a.add(...b) // result is set of {1, 2}
Problem is that set a does not contain value 3 after add function call.
This behavior have an explanation or it's more like a bug?
Thanks in advance
You can add create a new set like this:
let a = new Set([1])
let b = new Set([2, 3])
const c = new Set([...a, ...b])
Variable c contains all the items that you need.
The problem is that the add method of the set get only one parameter, no a arguments, if for that reason that only add one value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/add
If you do this:
let a = new Set([1])
a.add(2,3);
a will still only have 1,2 as elements. The spread operator is basically doing the same as above
This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 3 years ago.
When I Define variables in JS like this:
var a, b, c = 10;
I found out when call a,b,c this phrase (Or any other term that can be used) gives 10 value.
Now, i'm wondered and my question is what is that? and what type of this phrase? (Returned Number in console)
What is its use?
var a, b, c = 10;
a,b,c; //10
a, b,c; //10
The comma has 2 different means depending on context.
You use it in both these ways in your code sample - this is probably causing some confusion.
1) Multiple variable assignment using var
var a,b,c = 10;
Here the var keyword is used to assign multiple variables on a single line, in your case you only assign the final variable c, and so a and b are undefined.
Here is an example which makes its usage clearer:
var a = 1, b = 2, c = 3;
a; // 1
b; // 2
c; // 3
2) JavaScript Comma Operator
a,b,c; // 10
The JavaScript comma operator evaluates a comma separated list of expressions and returns the result of the last one.
This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 7 years ago.
In a coding test I have recently encountered a question that asks me to find out what will be printed in console. The question is below. I did not understand how this below code will be understood and executed.
Need help
var arr = ["a" , "b" , "c" , "d"][1,2,3];
console.log(arr);
What does that arr definition even mean?
Arrays in JS are one dimensional.
So var arr = ["a" , "b" , "c" , "d"][1,2,3];
Here [1,2,3] represents the indexes.
And comma is the least preceded operator so [1,2,3] will return you 3 (i.e. The right most element always).
Therefore
The result will return you the value at index 3 that's d
Update:
Let's see some more points for comma operator:
From MDN:
The comma operator evaluates each of its operands (from left to right)
and returns the value of the last operand.
So, It means:
var a = (10, 18);
a; //18
Also note that it's essential to mention them inside brackets while assigning to a variable.
If you try something like:
var a = 1,2,3;
a; // will return 1
So it's important that you wrap them using ().
When will you use it , operator?
You can use the comma operator when you want to include multiple
expressions in a location that requires a single expression. The most
common usage of this operator is to supply multiple parameters in a
for loop.
for (var i = 0, j = 9; i <= 9; i++, j--)
Now that you've understood about comma operator, Let's see few more examples to test our understanding:
1. var a = (1,3)/(2,1);
a // what will be the o/p
2. var a = ["Stack", "OverFlow","is","Awesome"][1,3,2][2,0]
a // What will be the o/p
3. var a = (true,0,1), b, c;
if(b,c,a) {
console.log("Will it execute?");
}
Your example caught me offguard. Here's another example which illustrates what is going on. Essentially a list of comma separated values evaluates to the value of the last.
if (false, false, true) {
console.log("hello")
}
// --> "hello"