Why do functions return multidimensional arrays, but the result is num?

when functions return multidimensional arrays (arrays are all num), but the result is num?

function show(){
    return [1,2][1,2];
}
function show1(){
    return [0,1,2][1,2];
}
function show2(){
    return [0,1,2,3,4,5,6,7,8,9,10,11,12][[0,1,2,[5,9,8,6][2,1,5,0],4,6][1,2,3]];
}
console.log(show());//undefined
console.log(show1());//2
console.log(show2());//5
+4
source share
2 answers

This is because you use a bracket to get an element from arrays.

As an example, the function show1returns 2 because:

[0,1,2]defines an array, and [1,2]is the bracket to get an element from the array. The code snippet is: [0,1,2][2]because the comma operator returns the last operand, which is 2.

You need to wrap a piece of code []to have an array with two elements:

[[0,1,2], [1,2]]
+7
source

. javascript ( ) 2 - , , javascript, :

var twoDimArray = [[1, 2], [3, 6]];
+2

Source: https://habr.com/ru/post/1650500/


All Articles