How To Set An Array

Table of contents:

How To Set An Array
How To Set An Array

Video: How To Set An Array

Video: How To Set An Array
Video: How to Set up a Mirrored Array in Windows 10 2024, November
Anonim

The object-oriented scripting language JavaScript is currently the most widely used object-oriented scripting language to make web pages interactive. Almost always, to solve any complex problems using this language, you have to resort to using arrays. The script starts using the array by declaring it. Let's see how exactly this is done.

Arrays in JavaScript
Arrays in JavaScript

Necessary

Basic knowledge of JavaScript

Instructions

Step 1

There are several ways to create a JavaScript array. For example, like this: var sampleArray = [element_0, element_1, element_2, element_2]; This line creates an array four units long. If you do not specify array elements, then an empty array will be created: var emptyArray =;

Step 2

Another way to define an array: var sampleArray = new Array (element_0, element_1, element_2, element_2); And this option will create an array four units long. And here, if you do not list the array elements, then the array will be created empty: var emptyArray = new Array (); In such an empty array, after the declaration, you can create several elements with indices that do not follow each other. For example: var emptyArray = new Array ();

emptyArray [4] = 47;

emptyArray [792] = 1; Unlike, for example, arrays in the C language, this array will occupy the amount of memory required to store only two elements, not 793. You can create an empty array of a given length, all elements of which will have a value " undefined ": var emptyArray = new Array (8); This empty array will contain 8 elements, whose value is undefined.

Step 3

For any of the above methods of creation, array elements can be integers or fractional numbers, string and logical values. Arrays can also be elements of other arrays. An example of creating an array containing one element of each of the listed types: var mixedArray = [4, 3.14, "text", true, [47, 8.1]; Since an array can be an element of another array, it is easy to understand how to create multidimensional arrays. An example of creating a three-dimensional array: var multiDimArray = [1, true], [8, true], 3.14], "text", 42]; These are all numbered arrays. Objects should be used to create associative (named) arrays in JavaScript.

Recommended: