JavaScript Arrays - Introduction in 2022 / Part 1

Photo by Andrew Neel on Unsplash

JavaScript Arrays - Introduction in 2022 / Part 1

·

3 min read

Introduction

Hello reader, if you are a web developer like me or wanted to become one then you always have to deal with JavaScript there is no other chance.

Today I want to make an introduction to arrays in JavaScript. So, maybe at least you can find the things that you still don't know about arrays.

What are the Arrays?

Let's make it simple for everyone,

Basically, you have a variable that can store multiple data inside and makes a list of data. That's what actually array is :)

That's a widely used data structure technique in computer science and almost every programming language has arrays. (Not all of them!)

What are the Arrays in JavaScript?

Arrays in JavaScript are a little bit different than arrays in other programming languages. If you check the definition you can understand the difference because they are a kind of special type objects in JavaScript.

Let's make it more simple to understand;

Normally arrays store only one kind of data type but multiple of them inside. For example, you can put multiple strings, integers, booleans, or objects into the array but only one type in one array.

But in JavaScript, you can put any kind of element into the array at once. (That's the difference).

How to create an Array in JavaScript?

There are multiple ways to create an array but you know what let's learn the generally used simple one because you don't need other ways to create an array unless you're developing a Framework or big libraries.

Just declare a variable, open square bracket, and ta-da you have a beautiful empty array like in the example below.

const myArray = [];

Let's make some fun :)

I am a little bit of a nerd and always want to check the type of the variables I have, let's check the type of this variable.

console.log(typeof myArray);

If you run this code probably you remember something like "arrays in javascript is a special type of objects" because you have a message like "object".

image.png

How to create an Array with multiple elements?

We actually created an array before but let's create another one and put 6 different data types into it.

const myArray2 = [1, 'string', true, {}, [], function hello() {console.log('hello')}]

If you check the array we created above, we put 6 different data types (integer, string, boolean, object, array, and function).

Did you read function? Be careful because you can put a function inside of the array in JavaScript.

How to print specific elements of the Array?

In order to print specific elements of the Array, you have to know the location of the element.

Remember that arrays are zero-based indexed data structures.

Let's make it simple;

  • The first element of the array is in the location 0.

  • The second element of the array is in the location 1.

  • The third element of the array is in the location 2.

Did you get the point? We always start to count the element's location from 0 and that's the reason that arrays are known as zero-based indexed data structures.

Simple example;

Print the fourth element of the array.

console.log(myArray2[3]);

image.png

If you check the image above, you can see that an empty object is printed because in our array we have the following elements.

 [1, 'string', true, {}, [], function hello() {console.log('hello')}]

// index 0 = 1
// index 1 = 'string'
// index 2 = true
--> index 3 = {}
// index 4 = []
// index 5 = function hello() {console.log('hello')}

That's all for array's introduction :)

Also, you can watch this tutorial on youtube;

Don't forget to follow me on;

Did you find this article valuable?

Support Olgun by becoming a sponsor. Any amount is appreciated!