0% found this document useful (0 votes)
26 views

JS Array

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
26 views

JS Array

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
213724, 0:56 AM chatcPT ChatGPT chat.openal.com/c/40604272-2860-4202-0800-e14ed61c30d1 In JavaScript, an array is a data structure that allows you to store multiple values in a single variable. Arrays can contain elements of any data type, including strings, numbers, objects, and even other arrays. Here's a basic overview of arrays in JavaScript: Creating an Array: You can create an array using square brackets [] and optionally initialize it with elements: Javascript let fruits = [‘apple', ‘banana’, ‘orange’ ]; Accessing Elements: You can access elements in an array using square brackets notation with the index of the element (indices start from 0): Javascript console. log(fruits[@]); // Output: ‘apple console. log(fruits[1]); // Output: ‘banana’ console. log(fruits[2]); // Output: ‘orange’ Modifying Elements: You can modify elements in an array by assigning a new value to a specific index: Javascript fruits[1] = ‘grape’; console. log(fruits); // Output: ['apple', ‘grape’, ‘orange'] ‘ntps:ifenatopenal.com'4080427a-836%-£242-9890-e14ed6 1380 18 213/24, 0:56 AM chatceT Array Methods: JavaScript provides several built-in methods for working with arrays: + push(): Adds one or more elements to the end of an array. + pop(): Removes the last element from an array and returns it + shift(): Removes the first element from an array and returns it * unshift(): Adds one or more elements to the beginning of an array. « splice(): Adds or removes elements from an array at a specified index. * slice(): Retums a shallow copy of a portion of an array into a new array. + coneat(): Combines two or more arrays. Javascript Jet numbers = [1, 2, 3]3 nunbers.push(4); // Adds 4 to the end nunbers.pop();// Renoves 4 fron the end nunbers.shift(); // Renoves 1 fron the beginning nunbers.unshiFt(@); // Adds @ to the beginning nunbers.splice(1, 1, 5); // Removes 1 element at index 1 and inserts 5 et slicesrray = nunbers.slice(1, 3); // Returns @ copy of elenents fron index 1 to 2 let conbinedArray = numbers.concat((6, 7]); // Conbines two arrays Iterating Through an Array: You can loop through the elements of an array using various methods such as for loop, foréach(), map(), ete, ntps:onat.openal.comil4060427a-2363-4262-9890-2 4odB1c3041 213724, 0:56 AM chatcPT Javascript // Using for loop for (let i= 0; i < fruits.length; itt) { console. log(fruits(i]); } // Using foreach() fruits. foreach(function(fruit) console.log(fruit); ns 1/ Using map() Let upperCaseFruits = fruits.map(function(fruit) ( return fruit.touppercase(); Ys console. log(upperCaseFruits) ; Multi-dimensional Arrays: Arrays in JavaScript can also contain other arrays, creating multi-dimensional arrays: Javascript let matrix = [ (1, 2, 31, [4, 5, 61, (7, 8, 9] console. log(matrix(@][1]); // Output: 2 Arrays are a fundamental part of JavaScript, and understanding how to use them effectively is essential for programming in JavaScript. ‘ntps:ifenatopenal.com'4080427a-836%-£242-9890-e14ed6 1380

You might also like