I have an array and I want to put same array and other array inside of the first array.
var arr=["a","b","c",arr,arr2];
var arr2=["a","b"];
var arr3=[];
arr3=arr[3];
When I print out arr, I can see
["a","b","c",,]
But if I print out arr3, The result is undefined.
How can I fix it?
2 Answers 2
You need to do this in the right order:
var arr1 = [];
var arr2 = [ "a", "b" ];
// Now arr1 and arr2 are defined so you can throw them into another array:
var arr3 = [ "a", "b", "c", arr1, arr2 ];
answered Nov 11, 2014 at 5:37
sanchez
4,5303 gold badges25 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You've defined arr as part of itself - arr[3] is arr. But at the time, arr doesn't exist yet, so it comes out as undefined.
answered Nov 11, 2014 at 5:35
Evan Knowles
7,5592 gold badges41 silver badges73 bronze badges
Comments
lang-js
arris undefined when you're creating the array that will be assigned toarr.undefinedbecause the variables are clearly not defined, no suprise there !