Arrays in Programming

- Andrés Cruz

En español
Arrays in Programming

Functions are one of the fundamental pillars in any programming language and JavaScript is no exception; It allows us to execute a set of instructions to perform a task, calculate a value and in a nutshell we can easily reuse a set of instructions by using functions.

To create a function we have to use the reserved word function followed by the name of the function and to indicate the name of the function you have to take into consideration the same aspects that we talked about when we defined the name of a variable, do not start with numbers, do not use special characters, spaces, etc.:

function multiplica() {
  console.log(5 * 2);
}

Here we simply create a function called multiply that is responsible for multiplying two values; so, if for some reason in life we need to multiply 5 * 2 multiple times in our program, we can perfectly create a function; but notice that we have only declared it, but at the moment it has not been executed, that is, the operation or set of operations, meaning instructions that we defined in it, have not been executed; to execute the function we simply have to place the name of the function and the parentheses: multiply()

Array indexes

As we saw previously, to access a particular element, we just have to enter a numerical value; for example:

carros[0]

This number is known as the index and with it we can access ALL the elements of the array, to access the first one, we have to place or use the index with a value of zero, the next one would be one:

var carros = ["Carro Corona", "Carro Fier", "Carro Safari", "Carro"]; 

carros[1] // aqui accedes a "Carro Fier"

And so for the rest of the elements.

Extra: Two-dimensional array

As I mentioned, there are multiple types of arrays, although arrays are not a scheme that you are going to use so much when developing most of your web projects, let's see a little how all this works.

If, as we have seen up to this point, an array is simply a list of elements, something sequential that goes in only one direction, then a two-dimensional array is an array of arrays, that is, an array with two dimensions (like a spreadsheet ); and here we have a very important point and that is that we can define multiple dimensions to an array, although again, in web development, we rarely need to use multi-dimensional arrays.

Array size

As with single-dimensional arrays (those presented previously), we can present the size we want the array to have, whether it is dimensional or not; for example:

matriz = [5][5]

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

In this case we would be creating a two-dimensional array of size 5x5; If we want a one-dimensional array of size 5:

matriz = [5]

* * * * *

Two-dimensional arrays in JavaScript - Matrices

To quote all this a little theoretically, in JavaScript, we can create two-dimensional arrays in the following way:

var matriz= [1, 1, 2, 3, 5, [1,2,3]];

In this case, we are simply creating an array, where one of its elements has another array; therefore, to access element 1 for example:

matriz[0]

Or to the array within the array:

matriz[5]

And we specify a particular value; For that we follow the same order, of specifying the index; for example, to access the last element:

matriz[5][2]
Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.