Understanding Basics of Array in JavaScript

Understanding Basics of Array in JavaScript

I will share some basics about Array. Array is the way of storing group or list of data. JavaScript arrays are used to store multiple values in a single variable. An array is a special variable, which can hold more than one value at a time.

In the post i will share some basics about Array.

Array is the way of storing group or list of data.

Let say, we want to store group of colors without using array.

    const color1 = 'purple';
    const color2 = 'black';
    const color3 = 'yellow';
    const color4 = 'aqua';

As we see, our codes are not DRY(Don't Repeat Yourself) at all. They are WET(Write Everything Twice), this is not a good practice for writing good code. We can use Array to solve this problem.

    const colors = ['purple', 'black', 'yellow', 'aqua'];

How to create Array

You can start with empty Array and then add data later, or you can start with it's data;

    // empty array
    const colors = [];

    // with data
    const colors = ['purple', 'black', 'yellow', 'aqua'];

Add data into array

There two way I known for adding data into the Array, 'bracket notation' and array methods.

i. By bracket notation

Array are indexed starting from 0.

    const colors = [];

    // Add first data
    colors[0] = 'purple';

    // So the second item
    colors[1] = 'black';

    console.log(colors); // ['purple', 'black'];

ii. By Array methods

If you want to add item at very first position of Array use unshift method, at very end use push method.

    const colors = ['purple', 'black'];

    // use unshift method to add to front
    colors.unshift('aqua');
    console.log(colors); // ['aqua', 'purple', 'black'];

    // use push method to add to end
    colors.push('yellow');
    console.log(colors); // ['aqua', 'purple', 'black', 'yellow'];    

Access data from Array

You can access data from Array by using bracket notation.

    const colors = ['purple', 'black', 'yellow', 'aqua'];

    // black and aqua
    colors[1]; // 'black'
    colors[3]; // 'aqua'

Also you can access array's item by loop over it.

    const colors = ['purple', 'black', 'yellow', 'aqua'];

    for(const color of colors) {
      console.log(color);  // purple black yellow aqua
    }

Update data of Array

Also you can use bracket notation to update array's data.

    const colors = ['purple', 'black'];

    // update black to yellow
    colors[1] = 'yellow';
    console.log(colors); // ['purple', 'yellow'];

Array can hold any data type and can be nested.

    const data = [24, true, ['orange', null], undefined];

javascript web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Web Design and Development Services

Get Best out from Web Design and Development Services from Vinew Technologies,We have a dedicated team of experienced and knowledgeable web developers, designers and testers. Therefore, we have proficiency in analyzing, developing and designing the necessity of intricate Website Development projects.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.