Array initializers allow us to declare and fill an array with specified values. We can also nest array initializers to create multidimensional arrays too. Below, I declare a one dimensional array of size 3 and a two dimensional array of size 3x3.
<!DOCTYPE html> <html> <head> <title>XoaX.net's Javascript</title> </head> <body> <script type="text/javascript" src="ArrayInitializers.js"></script> </body> </html>
var iaA = [3,7,1]; document.writeln("iaA["+ 0 + "] = " + iaA[0] + "<br />"); document.writeln("iaA["+ 1 + "] = " + iaA[1] + "<br />"); document.writeln("iaA["+ 2 + "] = " + iaA[2] + "<br />"); document.writeln("<br />"); var iaaM = [[1, 3, 4], [2, 1, 4], [3, 2, 1]]; document.writeln("[ " + iaaM[0][0] + " " + iaaM[0][1] + " " + iaaM[0][2] + " ]<br />"); document.writeln("[ " + iaaM[1][0] + " " + iaaM[1][1] + " " + iaaM[1][2] + " ]<br />"); document.writeln("[ " + iaaM[2][0] + " " + iaaM[2][1] + " " + iaaM[2][2] + " ]<br />");
© 20072024 XoaX.net LLC. All rights reserved.