JavaScript Arrays - Introduction in 2022 / Part 1

Search for a command to run...

No comments yet. Be the first to comment.
Last week, I encountered issues with my existing Ubuntu setup. It was slow and lagging, and I couldn't resolve the problem. Then, I remembered that my laptop had two operating systems installed: Windows and Linux on separate partitions. Suspecting a ...
Introduction If you want to learn about how to use console.log as a professional developer then you have to learn computer property names. Let's Start Assume that we have 4 different objects and each one is assigned to a variable. How can we log thos...

Hello artisans, Today I'll be writing about how to use Query Scopes efficiently in Laravel projects. Query Scopes are very useful to encapsulate the logic you want to apply to database queries while using Models. Let's start with an example and under...

Laravel Sanctum is one way to authenticate different models to use your application but there are many other ways. In this article, I'll be explain how to authenticate team/team members with a Laravel Sanctum. Assume that we have following Model; cla...
Introduction: Managing forms in web applications can be a complex task, especially when dealing with validation rules, error handling, and form submissions. The React useForm plugin library offers a powerful and elegant solution to simplify form mana...

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.
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!)
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).
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 = [];
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".

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.
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]);

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;