Skip to main content

Command Palette

Search for a command to run...

JavaScipt Arrays

Published
2 min readView as Markdown
JavaScipt Arrays
T

I'm a recent graduate with a degree in Mechanical Engineering but I have a passion for web development. I have hands on in ReactJS and I have experience building responsive, user-friendly web applications using this framework. I gained hands-on experience in web development through several projects . I'm proficient in HTML, CSS, JavaScript, and I have experience working with various front-end technologies such as ReactJS, Redux, and Bootstrap. I'm also familiar with back-end development such as Node.js and Express.js.

An array is a special variable, which can hold more than one value.

or

an array is a data structure used to store multiple values in a single variable. It is an ordered collection of elements, where each element can be accessed by its index or position within the array.

Different ways to create Arrays

Using Assignment operator: It is most common way to create an array in JavaScript would be to assign that array to a variable like this

const cars = ["Saab", "Volvo", "BMW"];
console.log(cars); // ["Saab", "Volvo", "BMW"]

Using Array constructor: Another way to create an array is to use the new keyword with the Array constructor. the output will same

const cars = new Array("Saab", "Volvo", "BMW");
console.log(cars); // ["Saab", "Volvo", "BMW"]

Using Array.of(): Another way to create an array is to use the Array.of() method. This method takes in any number of arguments and creates a new array instance.

const cars = Array.of("Saab", "Volvo", "BMW");
console.log(cars); // ["Saab", "Volvo", "BMW"]

Using split() method: The split() method splits a string into an array of substrings.

const cars = "Saab Volvo BMW";
console.log(cars.split(" ");  // [ 'saab', 'volvo', 'bmw' ]

Accessing Array Elements

You access an array element by referring to the index number:

const cars = ["Saab", "Volvo", "BMW"];
console.log(cars[0]); // "Saab"

Note: Array indexes start with 0.

[0] is the first element. [1] is the second element.

Changing an Array Element

This statement changes the value of the first element in cars:

const cars = ["Saab", "Volvo", "BMW"];
 cars[0]="Audi";
console.log(cars); //  ["Audi", "Volvo", "BMW"]

Arrays are Objects

Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.

But, JavaScript arrays are best described as arrays.

Arrays use numbers to access its "elements".

Adding Array Elements

const cars = ["Saab", "Volvo", "BMW"];
cars.push("Audi");
console.log(cars); // ["Saab", "Volvo", "BMW","Audi"]

In JavaScript, arrays always use numbered indexes.

const person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
person.length;    // Will return 3
person[0];   // "John"
console.log(person) // [ 'John', 'Doe', 46 ]

More from this blog

J

JSpoint

53 posts

Discover the power of JavaScript with our comprehensive website. Explore resources and practical examples to enhance your coding skills. From frontend development to backend scripting.