This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 8 years ago.
Using Javascript in Firefox, Safari and Chrome (didn't try IE), the following strange behavior happens:
data = new Array(1,3,5,7,9);
data[1,7,3] = 88;
alert( data ); // displays: 1,3,5,88,9
So apparently the value at index [3] has been changed (other tests show that it actually has changed data[3]).
The second command does not generate an error. It does not even generate a warning in NetBeans, nor an error when displayed in a browser.
I explored this a bit further. It appears that :
data[1,7,null,NaN,4,3]
also gets interpreted as data[3] -
this also works with other values than 3.
The last value in the list is used and the rest are ignored.
Does this behavior have some sort of meaning or purpose, or is it just an unexpected fault in the parser?
I was unable to find any documentation or explanation of this behavior/syntax.
You're using the comma operator.
<expr1>, <expr2>, <expr3>, ...
is an expression that evaluates each expression from left to right, and returns the value of the last one. So 1,7,3 evaluates to 3. So
data[1,7,3] = 88;
is equivalent to:
data[3] = 88;
Related
This question already has answers here:
What is "undefined x 1" in JavaScript?
(5 answers)
What's the difference between empty items in a JavaScript array and undefined? [duplicate]
(2 answers)
Closed 3 years ago.
Can anyone help me understand what an empty means in a JS array?
I just noticed an array in chrome devtools with an empty value. I created the one below, and by putting two commas together I can produce an empty (see below). Granted, I'm relatively new to JS, but since empty isn't a JS primitive I would have expected either:
a) a null or undefine3d value for a[3]
b) a.length to be 4
What does empty mean?
Array literals can include elisions, e.g.
[0,1,,3];
The two commas together mean "there is no element at index 2". Many stringified versions will show undefined at index 2, however that is misleading as there is no element at all at that position.
E.g.
let arr = [0,1,,3];
arr.forEach((v, i) => console.log(`index ${i} has value ${v}`));
console.log('stringified: ' + arr);
console.log('SO console:', arr);
console.log('JSON: ' + JSON.stringify(arr));
An alternative is to show "empty", which (IMHO) isn't really suitable either, it should be shown as in the literal (i.e. as for "stringified").
This question already has answers here:
Sorting Javascript Array with Chrome?
(4 answers)
Closed 4 years ago.
const arr = [{name:'Suzanne'},{name:'Jim'},{name:'Trevor'},{name:'Amanda'}];
arr.sort((a,b)=>a.name > b.name);
I want to sort elements(objects) in the array name-alphabetically(A->Z). When I use the code in node(v8.4.0) and safari development tool(console), it works well.
But it doesn't work in chrome development tool-console(v70.0.3538.110). There is a result of code in chrome console.
In my opinion, the code is not wrong. I don't know why the code doesn't work well.
The .sort() callback must return a number, not a boolean. The return value should be:
a negative number if the first element should go before the second;
a positive number if the first element should go after the second;
zero if the two elements are ordered the same.
For strings, you can use the .localeCompare() method:
arr.sort((a,b) => a.name.localeCompare(b.name));
This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 5 years ago.
I am stuck with a part of my google script code where one of array elements changed unexpectedly. It looks like a bug to me, but I'd like to hear from here if it's really is or just my coding error that I overlooked. Of course, I'm hoping for a solution as well in any case.
Here is that part of the code.
if (chkIn) {arr[1] = importData[i][1]+'2';
} else {
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
arr[1] = importData[i][1]+'1';
Logger.log('---------------------------------------------------');
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
};
(The if statement doesn't seem relevant here, but I included it just in case.)
Here is the output.
2573 2017122103
2574 20171221041
2575 20171221042
20171221042
---------------------------------------------------
2573 2017122103
2574 20171221041
2575 20171221031
20171221031
I really have no idea how importData[i+2][1] changed its value to arr[1] (the number after 2575).
Thank you in advance.
Probably this is because in your case:
arr === importData[i+2]
So when you change arr[1] you also have changed importData[i+2][1].
This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 5 years ago.
I'm using https://js.do/ as a sandbox for simple scripts and document.write to print results. I'm working with multidimensional arrays, with code such as
var x = [];
x[1, 2] = 0;
However, I'm a little confused on what exactly document.write is printing.
On researching multidimensional arrays in JS, I found no mention of the notation used above, but rather x[1][2] was used in the examples found instead (ie. an array inside of an array).
I can't remember where I first came across the above way of using multidimensional arrays - perhaps someone could provide a link to enlighten me?
x[1, 2] = 0; assigns 0 to index 2 of x, where comma operator , evaluates last value of 0, 2 expressions as an index of x at bracket notation
var x = [];
x[1, 2] = 0;
console.log(x[2] === 0);
The syntax with the comma is incorrect, but apparently doesn't cause a syntax error. It is being interpreted by simply taking the last value, so the example x[1,2] = 0 is being views as x[2] = 0. That creates an array with 0 in the third position (index 2), [undefined,undefined,0]. As that gets written to the DOM, the undefined is ignored, but a comma is still added. So your output is ,,0.
This question already has an answer here:
Unexpected behavior using Array Map on an Array Initialized with Array Fill [duplicate]
(1 answer)
Closed 6 years ago.
I know that this is the future technology, but MDN doesn't cover all use-cases, ans almost any environment already supports this method. Here's an example I am worried about:
var groups = new Array(4);
groups.fill([]);
groups[0].push(1);
groups[0] // --> [ 1 ]
groups[1] // --> [ 1 ]
So it's not just fills groups with empty array, it fills groups with the same one empty array!
Is this an intended behavior or things can change in the future?
It is the intended behavior according to the spec. The value is not cloned.