Arrays in Javascript

We see what arrays are in Javascript, what they are for and how to use them. We will see various ways to create them, as well as define and access their values.

We move on to a new topic in , in which we are going to learn about our first data structure.

In programming languages ​​there are special data structures that are used to store more complex information than simple variables. A typical structure in all languages ​​is the Array, which is like a variable where we can introduce several values, instead of only one as it happens with normal variables.

Arrays allow us to store several variables and access them independently, it’s like having a variable with different compartments where we can enter different data. For this we use an index that allows us to specify the compartment or position to which we are referring.

Note: Arrays were introduced in Javascript versions 1.1 or higher, that is, we can only use them starting from browsers 3.0. For older browsers you can simulate the array using object-oriented programming syntax, but the truth is that currently this limitation should not concern us. Furthermore, given the complexity of the task of simulating an array by means of objects, at least at the moment in which we find ourselves and the few occasions in which we will need it, we think that it is better to forget about it and just work with arrays. usually. So in this article and the ones that follow, we are going to see how to use the real Javascript array.

See also  Create your first Progressive Web App with Ionic

javascript array creation

The first step in using an array is to create it. For this we use a Javascript object already implemented in the browser. We will see from now on a topic to explain what object orientation is, although it will not be necessary to understand the use of arrays. This is the statement to create an array object:

var myArray = new Array()

This creates an array on the page that is running. The array is created without any content, that is, it will not have any cells or compartments created. We can also create the Javascript array by specifying the number of compartments it will have.

var myArray = new Array(10)

In this case we indicate that the array is going to have 10 positions, that is, 10 cells where to save data.

It is important that we notice that the word Array in Javascript code is written with the first letter in uppercase. As in Javascript the uppercase and lowercase do matter, if we write it in lowercase it will not work.

Whether the number of cells of the javascript array is indicated or not, we can enter any data in the array. If the box is created, it is simply entered and if the box was not created, it is created and then the data is entered, with which the final result is the same. This checkbox creation is dynamic and occurs at the same time that the scripts are executed. Next, let’s see how to introduce values ​​into our arrays.

See also  Javascript transpilation with Webpack and Babel

myArray = 290 myArray = 97 myArray = 127

They are introduced indicating between square brackets the index of the position where we wanted to save the data. In this case we introduce 290 in position 0, 97 in position 1 and 127 in position 2.

Arrays in Javascript always start at position 0, so an array that has, for example, 10 positions, will have cells from 0 to 9. To collect data from an array, we do the same thing: putting the index of the position to between brackets. which we want to access. Let’s see how the contents of an array would be printed on the screen.

var myArray = new Array(3) myArray = 155 myArray = 4 myArray = 499 for (i=0;i<3;i++){ document.write("Position " + i + " of array: " + myArray) document. write("
“) }

We have created an array with three positions, then we have entered a value in each of the positions of the array and finally we have printed them. In general, traversing arrays to print their positions, or anything else, is done using loops. In this case we use a FOR loop that goes from 0 to 2.

Can .

Data types in arrays

In the cells of the arrays we can store data of any type. We can see an array where we introduce character data.

myArray=”Hello” myArray=”to” myArray=”everyone”

Even in Javascript we can store different types of data in the cells of the same array. That is, we can enter numbers in some boxes, texts in others, Booleans or whatever else we want.

myArray = “webdevelopment.com” myArray = 1275 myArray = 0.78 myArray = true

See also  How to write the EURO special character in HTML

Summary declaration and initialization of Arrays

In Javascript we have at our disposal an abridged way of declaring an array and loading values ​​in the same step. Let’s look at the following code:

var fastarray =

As you can see, we are defining a variable called arrayRapido and we are indicating in the brackets several values ​​separated by commas. This is the same as declaring the array with the Array() function and then loading the values ​​one by one.

In the next article we will continue to see things related to arrays, specifically we will learn how to .

Loading Facebook Comments ...
Loading Disqus Comments ...