JeffLearnsReact – 6- Arrays

Array methods – functions that manipulate array contents. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array let myArray = [‘item one’, ‘item two’, ‘item three’] const myArray = [‘item one’, ‘item two’, ‘item three’] if you use const, you cannot change the data type but you CAN manipulate the array in any way. Each item has a numbered position JS starts counting […]

Array methods – functions that manipulate array contents.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

let myArray = [‘item one’, ‘item two’, ‘item three’]
const myArray = [‘item one’, ‘item two’, ‘item three’]

if you use const, you cannot change the data type but you CAN manipulate the array in any way.

Each item has a numbered position

JS starts counting from 0

myArray[0] gets the first item

you can change the value of an item
myArray[0] = ‘first one’;

get the number of items with .length
myArray.length

add items to the beginning of the array with .unshift
myArray.unshift(‘item zero’)

add items to the end of the array with .push
myArray.push(‘item four’, ‘item five’);

remove the first item of an array with .shift
myArray.shift();

remove the last item of the array with .pop
myArray.pop();

get a section of items with .slice
myArray.slice(1,4); // 2nd thru 5th items

join(), .slice(), .splice(), .shift(), .unshift(), and .concat()

NOTE: Strings are similar
let hello = ‘Hello World’;
console.log(hello[6]); // Output: W

Powered by WPeMatico