Skip to main content

JavaScript arrays and methods

JavaScript arrays and methods

Hello guys. Welcome to another lesson on JavaScript. In the previous lesson, we were looking at JavaScript objects and methods. We saw how to declare objects, access the various object properties, and how to make use of methods. Here in this lesson, we will be studying arrays and methods. You will get to know about arrays and how to play around with them. Just stay focused as you learn.

An array in JavaScript is an ordered list of values, and each value is called an element. Arrays are capable of holding values of different data types. They are dynamically sized, meaning you don’t necessarily need to specify the array size upfront because they can automatically increase or decrease in size depending on the operations that are being carried out with them.

To declare an array, we make use of square brackets. Let us take an example by declaring an array of fruits.

Var fruits  = [“apple”, “orange”, “avocado”, “mango”];

The elements of the above array can be accessed making use of their index numbers. For example, to access the first element, we type the following line of code.

Fruits[0]

Meaning, the first element of any array (apple) has an index of 0, then the next has an index of 1 and so on. Let’s have a view of the output of the code.

JavaScript arrays and methods

We could also declare an array using round brackets, but it is preferable to use square brackets.

Now let’s take a look at some of the major things we can do with arrays. There are so many things you can do to/with an array. You can remove elements from an array, add elements to an array, replace an element, change the array size, and so on. All of these are possible with the use of array methods. 

Array methods are built-in JavaScript functions that being used on arrays. Each method has a unique function which could be a change or calculation. It also saves us from writing our own functions from scratch.

Below are some most commonly used array methods. There are tons of other array methods that are being made use of in JS.

fruits.pop(); //Removes the last element from the array
fruits.sort; // Sorts the elements in the array
fruits.push(); //Adds new element at the end of the array

Let is try out the first object method above. Writing it gives the following output:

JavaScript arrays and methods
Looking at the fruits array after writing the line of code with the “pop()” array method, you see that the last element which is “mango” has been removed from the array.

One other important method is the “concat()” which joins 2 or more arrays together. Let’s declare another array and join its elements to our previous array. 

Declaring the new array:

var fruits1 = [“Banana”, “Avocado”, “Grape”];

Now let us join the two arrays together :

var fruits = [“apple”, “orange”, “avocado”, “mango”];
var fruits1 = [“Banana”, “Avocado”, “Grape”];
var fruits_new = fruits.concat(fruits1);

The code gives the following output when we view the new array.

JavaScript arrays and methods
You see how exciting JavaScript can be right? We are here to help you master the language so that you can be able to do amazing stuff with it. Stay tuned for more upcoming lessons. Happy coding.

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...