Creating arrays and accessing elements

Creating arrays and accessing elements

Array is a user defined data type in JavaScript programming, which stores and processes homogenous data type values in a particular order sequence. Array data type is used to process and manipulate integer, character, string, floating, data type values in sequence. By default, array data type is stored in zero-indexed order in storage. This means that the first element of the stored array is stored at index location 0, the second element of the stored array is stored at index location 1.

Creating arrays and accessing elements

Creating array in JavaScript.

Array data type can be created in many ways in JavaScript program. JavaScript programmers can construct the desired array using array literals and array constructor method in a program.

Usage of Literals in JavaScript Array.

It is common to use literals in the basic method to create array in JavaScript program. Where programmers can declare multiple values for array by using empty square brackets ([]) in array method.

let programming = [“Html”, “Css”, “Javascript”, “Node.js”];

console.log(programming); // Result is – [ ‘Html’, ‘Css’, ‘Javascript’, ‘Node.js’ ]

Explanation of above array.

Here an array named programming is created with default 4 array elements declared. “Html”, “Css”, “Javascript”, “Node.js”.

Use of array constructor method in JavaScript.

JavaScript programmers can create and process any user defined array element by using array constructor method in the program.

let integer = new Array(9, 8, 7, 3, 2,1);

console.log(integer); // Result – [ 9, 8, 7, 3, 2, 1 ]

Explanation of array constructor method.

Here in this program array constructor method can be used to create an array element by passing the array values that programmer wants to add to the array.

Use of Array.of() method in JavaScript.

In JavaScript program array.of() method is used to create a new array instance with a variable number of array elements defined as array arguments.

let laptop = Array.of(“Mackbook”, “Hp”, “Dell”, “Lenovo”);

console.log(laptop); // Result-[ ‘Mackbook’, ‘Hp’, ‘Dell’, ‘Lenovo’ ]

Explanation of Array.of() method.

Array.of() method in a JavaScript program is used when the programmer needs to create an array with a specific value, and it does not explain the array element arguments like the array constructor does, i.e., when only one array element argument is passed, it will not create an array of a particular length.

Use of Array.from() Method in JavaScript.

Array.from() method in JavaScript program creates a new array instance from an array-like or iterable array object containing strings, set.

let str = “vcanhelpsu”;

let chars = Array.from(str);

console.log(chars); // Result -[‘v’, ‘c’, ‘a’, ‘n’,’h’, ‘e’, ‘l’, ‘p’,’s’, ‘u’]

Explanation of Array.from() Method.

In this program, the Array.from() string method converts the str data type into an array of individual characters and displays it.

Accessing Elements in a JavaScript Array.

Using array indexing in a JavaScript program, you can access and process any created array element individually or one by one. By default, array elements created in JavaScript programs are stored in zero-indexed order, which means the first array element storage index location is set to 0 by default.

Using array indexing in JavaScript programs.

To access any created array element in a JavaScript program, use the name of the currently declared array followed by the indexing of the array element given in square brackets ([]).

let development = [“python”, “java”, “javascript”, “matlab”];

console.log(development[0]); // Result – python

console.log(development[1]); // Result – java

console.log(development[2]); // Result – javascript

console.log(development[3]); // Result – matlab

Explanation of array indexing.

In this program development[0] accesses and previews the first array element (“python”) from the index location, development[1] accesses the second array element (“java”), and development[3] accesses and previews the third array element (“javascript”) and the fourth development[3 ] accesses and previews the array element (“matlab”).

Accessing the last array element in JavaScript.

Javascript programmers can use the length properties of an array to access the last stored array index location without knowing the exact index location of the array.

let software = [“windows”, “ms office”, “javascript” , “Linux”];

console.log(software[software.length – 1]); // Result – Linux

Explanation of accessing the last array element.

Here in this program software.length – 1 attributes displays accessing the last array index element location in the current array.

Accessing elements with negative indexes in JavaScript using the array’s at() method.

In JavaScript, ES2022 introduced the at() method which allows negative indexing in arrays. Where negative indexing in the created array starts from the end element of the array, with -1 being the last stored array element.

let country = [“India”, “Usa”, “Uk”, “Australia”];

console.log(country.at(-1)); // Result – Australia

console.log(country.at(-2)); // Result – Uk

console.log(country.at(-3)); // Result – Usa

Explanation of negative indexes in JavaScript.

Here in this program, the at() method allows accessing negative array indexes. Here the at(-1) method returns the last array element (“Australia”) stored array element value, and at(-2) returns the second-to-last array element (“Uk”) array value.

Accessing elements from nested array in JavaScript.

A JavaScript program can have an array nested within another array or an array within an array. JavaScript programmers can access elements in a declared nested array by adding square brackets for individual levels of array nesting.

let arrayinsidearray = [4, 7, [10, 7], 11];

console.log(arrayinsidearray[2][0]); // Result – 10

console.log(arrayinsidearray[2][1]); // Result – 7

Explanation of nested array.

In this program, arrayinsidearray array has another array element at index 2. To access the nested array element declared here, JavaScript programmers use two square brackets. One for external array and one for internal array element.

Modifying array elements in JavaScript.

JavaScript programmers can modify array elements by directly accessing them through their index location and indicating a new array value.

let language = [“hindi”, “english”, “regional”];

language[1] = “telgu”; // it use to Modify the second array element value

console.log(language); // Result – [ ‘hindi’, ‘telgu’, ‘regional’ ]

Explanation of modifying array elements.

Here in this program, the second array element (“english”) is changed to “telgu” language by giving a new value to language[1].

Array methods for accessing array elements.

JavaScript programming provides programmers with several options to access and manipulate array elements.

shift() – Here the shift() function deletes the first element of a JavaScript array and returns the remaining array value.

let course = [“o level”, “mca”, “bca”];

let value = course.shift();

console.log(value); // Result – o level

console.log(course); // Result – [ ‘mca’, ‘bca’ ]

pop() – This array function deletes the last element of the array and returns the array value.

let course = [“o level”, “mca”, “bca”];

let value = course.pop();

console.log(value); // Result – o level

console.log(course); // Result – [ ‘mca’, ‘bca’ ]

slice() – slice() in a JavaScript program returns an array copying values of a part of an array into a new array.

let course = [“o level”, “mca”, “bca”];

let slicedcourse = course.slice(1, 3); // here it Extracts elements from index 1 to 2 excluding index 3

console.log(slicedcourse); // Result – [ ‘mca’, ‘bca’ ]

indexOf() – This function returns the index of the first occurrence of an indicated value in the current program.

let course = [“o level”, “mca”, “bca”];

let index = course.indexOf(“mca”);

console.log(index); // Result – 1

find() – It returns the first element value in the declared array that satisfies the given array testing function.

let integer = [100, 90, 399, 783, 999];

let found = integer.find(int => int > 1);

console.log(found); // Result – 100

Summary of array in JavaScript programming.

  • Creating an array – JavaScript programmers can create the desired array using array literal square brackets ([]), array constructor (new Array()), or popular methods like Array.of() and Array.from().
  • Accessing elements – In JavaScript programs, the index location can be used to access the array elements in the created array, in which the first array element is at index location 0. Similarly, you can also use negative index features with at() method to access array elements from the end of the array.
  • Modifying elements – In JavaScript programs, you can modify array elements by directly indicating new values at specific index locations.
  • Array Methods – In JavaScript programs, you can use built-in functions or methods like shift(), pop(), slice(), indexOf(), and find() to manipulate specific array elements and access those array elements.

Leave a Reply