Arrays in JavaScript
Similar to most programming languages, arrays in javascript are also created to store multiple data in a single variable, removing the tedious task of declaring multiple variables for multiple different data.
In JavaScript, there are primitives and objects. Simply put, primitives have no methods and properties, while the objects in js do. Primitives of js are immutable, meaning their current values cannot be altered(not be mistaken with assigning a new value to the existing variable but manipulating the current value to make something useful). In contrast, on the other hand, objects of js have more properties and methods, are mutable and offer many operations to be performed in them than primitives.
Arrays are objects. They offer many useful methods and properties while developing real-world applications. Some of which are going to be discussed in the article below. It's important to know how each method works to clear the basic understanding of arrays in JavaScript.
Declaring an array:
Declaring an array in JavaScript is simple. It just requires some changes to the syntax of a normal variable declaration, and arrays are ready to be used!!
Syntax:
let var_name = [data1, data2, data3,......,datan];
let names = ['aayush','john','eminem','taylor','bipul',1,2,3];
console.log(names);
Seeing 1,2,3, and a bunch of strings together in the above code might make you wonder if that is even possible, with JavaScript say no more. Yes, we can declare data of different datatypes in the same variable in JavaScript.
With a basic understanding of how arrays in js operate and how they are declared, we are ready to dive deep into the world of methods and properties it offers. So, let's get started with using them.
Index of arrays:
The data in arrays are assigned different indexes. The first data is assigned an index of 0, the second with 1, and so on.
For example:
let name = ['aayush','eminem','taylor','sam'];
console.log(name[0]);
//This will print aayush as it holds the 0 index of array
console.log(name[1]);
//This will print eminem as it has the index of 1
console.log(name[3]);
//It prints sam as it in 3rd index
//And you get the idea of indexes
Methods and Properties of JS:
Different array methods in JS make coding much easier, especially for beginners, because many things can be achieved with a single line of code. Let's take a look at some of them.
1. Length of array:
The length of an array can be determined using this method. It counts the number of data present in an array. Not to be mistaken with the index, it starts the count from 1 and gives the exact data.
Example:
let name = ['aayush','eminem','taylor','sam'];
console.log(name.length);
//Output = 4
Now that you know how to find the length of an array, imagine you have to print the last value from an array, but you don't know the length.
Sounds complex?? IT REALLY ISN'T!!
Let's make use of what we just learned. We know how to print an array if we know the index, and we also learned how to get the full length of an array. So let's solve it.
let name = ['aayush','eminem','taylor','sam'];
console.log(name[3]);
//We know that this is going to print sam because it is in index 3
//It is also the last value of this array
//But imagine we dont know that 3 is the last index.then?
console.log(name[name.length-1]);
//This prints sam as well
If the above concept is clear, we are heading towards finally learning arrays and their methods.
If you didn't, however, don't worry. Here's the explanation:
We know that typing an index of an array prints what is present in that index.
name[name.length-1]
Using name.length, we got the whole length of an array. But as we know, it gives 4, counting from 1, whereas the index starts from 0, so we subtract one to get the last value.
2.Push():
Using the push method, we can add an element at the end of an array.
let name = ['aayush','eminem','taylor','sam'];
name.push('rohit');
console.log(name);
//Output:
//'aayush', 'eminem', 'taylor', 'sam', 'rohit'
//it has added rohit at the end of an array
3.sort():
Using the sort method, we can sort the strings in alphabetical order and numbers in numerical order.
let name = ['aayush','eminem','taylor','sam'];
console.log(name.sort());
//output : aayush, eminem, sam, taylor
let num = [9,3,6,2,8,7,4,1,5];
console.log(num.sort());
//output : 1, 2, 3, 4, 5, 6, 7, 8, 9
Remember we learned that we can initialize numbers and strings in the same variable?? So what happens if we have strings as well as numbers and then use the sort method? Well, lets find out.
let name = ['aayush','eminem',3,'taylor','sam',8,1,4];
console.log(name.sort());
//output : 1, 3, 4, 8, aayush, eminem, sam, taylor
Hence, it first sorts numbers and then strings.
4.reverse():
To reverse an array, we use reverse() method.
let maths = [ 1,4,9,16,25];
console.log(maths.reverse());
//output: 25, 16, 9, 4, 1
5.isArray():
It checks whether the passed value is an array or not. It gives the result in true or false, true, if it is an array and false if not.
console.log(Array.isArray([1,5]));
// output : true
console.log(Array.isArray(5));
//output : false
For more JavaScript Methods, make sure to follow me. There is going to be a part 2 of arrays in which we will discover more methods about arrays.