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

arrays[1]

The document discusses JavaScript arrays, including their ability to hold any data type and methods for transforming and copying arrays. It explains the use of the map function, how to push new elements, and different ways to copy arrays using slice and the spread operator. Additionally, it introduces the rest operator for merging multiple arguments into an array.

Uploaded by

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

arrays[1]

The document discusses JavaScript arrays, including their ability to hold any data type and methods for transforming and copying arrays. It explains the use of the map function, how to push new elements, and different ways to copy arrays using slice and the spread operator. Additionally, it introduces the rest operator for merging multiple arguments into an array.

Uploaded by

pavanyonex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

arrays

It can have any data type

const hobbies=['sports', 'cooking'];

for (let hobby of hobbies) {


console.log(hobby);
}

map - it takes a function on how to transforms the values and return a new array

console.log(hobbies.map(hobby=> 'hobby: '+ hobby));


it will be executed on every element on after the other
//single args so no ()
or

console.log(hobbies.map(hobby => {
retun 'hobby:' +hobby;
}));

hobbies.push('programming');

Note: always copy and edit the array instead of adding new array to the existing
one
ways to copy the array

1. slice copies the entire array without args

const copiesArray = hobbies.slice();

copies only 2 elements


const copiesArray = hobbies.slice(2;

it create a nested array


const copiedArray = [hobbies]

2. using spread operator

const copiedArray = [...hobbies];


console.log(copiesArray);

const person = {
name: 'pawan',
age: 12,
greet() {
console.log('hi there');
}
};

const copiedPerson ={...person};


console.log(copiedPerson);

rest operator

merge multiple args into an array and used in argument list of an function
const toArray = (...args) => args;
console.log(toArray(1,2,3,4));

You might also like