Arrays

·

3 min read

Arrays are collections of items. you can consider them as variables which can store more than one value. you can store different data types in arrays. they are similar to lists in python. arrays are stored inside square brackets, each value separated by commas.

syntax:

It is a common practice to declare arrays with the const keyword.

const usernames=["ravish11","bodmas43","nousername","shevata19","dao"]

To access an array element you have to refer to the index number of an array. index numbers start from 0 and not from 1.

const usernames=["ravish11","bodmas43","nousername","shevata19","dao"]
console.log(username[3]) // it will give you the username "shevata19"

Arrays are a special type of object. The typeof() operator in JavaScript returns "object" for arrays they are not primitive data types like strings.

Methods and Properties in arrays:

methods are set of instructions that perform a task. they are similar to functions. the only difference is, the method is associated with an object and the function is not.

arrays are mutable and the elements inside of an array can be updated.

.length: it is a property and not a method. length gives us the number of elements present inside an array.

const usernames=["ravish11","bodmas43","nousername","shevata19","dao"]
console.log(usernames.length)//length property don't need parantheses.

.toString: converts an array to a string.

const usernames=["ravish11","bodmas43","nousername","shevata19","dao"]
let usernameString = usernames.toString()

.join(): creates a new string but with a specified selector like comma or space.

const usernames=[,"ravish11","bodmas43","nousername","shevata19","dao"]
let loginInfo= usernames.join("\n user: ")

.pop(): removes the last element from an array. returns the value that was removed.

.push(): adds a new element at the end of an array. returns the new array length.

.shift(): removes the first element from an array. it returns the value that was removed.

.unshift(): adds a new element at the start of an array. returns the new array length:

const usernames=["ravish11","bodmas43","nousername","shevata19","dao"]
let poppedItem = usernames.pop()
let pushedItem = usernames.push("daonew")
let shiftedItem = username.shift()
let unshiftedItem = username.unshift("ravishnew11")

.concat(): creates a new array by merging existing arrays. you can concatenate as many arrays. it can also take strings as an argument

const usernames=["ravish11","bodmas43","nousername","shevata19","dao"]
const passwords= [1024,3452,5434,8976,4566]
const loginInfo = usernames.concat(passwords]

delete: delete is an operator, not a method. it is used to delete an array element by giving an index number. Using delete leaves undefined holes in the array. it does not affect the length of an array.

const toDelete = [22,34,55,67,778,97,56,465,45,776,42]
delete toDelete[9]

.reverse(): reverses the complete array.

const numbers = [22,34,55,67,778,97,56,465,45,776,42]
const reversedArray = numbers.reverse()

.sort(): sorts the array alphabetically. by default sort() function sorts values as strings and not as numbers.

const usernames=["ravish11","bodmas43","nousername","shevata19","dao"]
const sortedUsernames = usernames.sort()

to sort numbers inside of an array you have to compare function.

const numbers = [22,34,55,67,778,97,56,465,45,776,42]
numbers.sort(function(a, b){return b - a}) //this will sort an array in descending order.

.splice(): adds new items in one go. splice() method takes at least 3 parameters.

the first parameter defines the position where new elements should be added.

the second parameter defines how many elements should be removed.

rest of the parameters are the elements that should be added.

const numbers = [22,34,55,67,778,97,56,465,45,776,42]
const newNumbers = numbers.splice(0,2,51,52)
// if you console.log the newNumbers it will return the removed elements and not new list of added array elements

.slice(): slices out a piece of an array into a new array. The slice() method does not remove any elements from the source array. it can take two arguments first one selects elements from the start argument and the second one, the end argument (exclusive). if you exclude the second argument then it'll slice out the rest of the array.

const numbers = [22,34,55,67,778,97,56,465,45,776,42]
const newNumbers = numbers.slice(5,9)