0% found this document useful (0 votes)
5 views3 pages

Typescripts

The document provides examples and explanations of various JavaScript array methods including splice, push, pop, slice, and split. It demonstrates how to manipulate arrays by adding, removing, and accessing elements using these methods. Each method is illustrated with code snippets and expected outputs to clarify their functionality.
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)
5 views3 pages

Typescripts

The document provides examples and explanations of various JavaScript array methods including splice, push, pop, slice, and split. It demonstrates how to manipulate arrays by adding, removing, and accessing elements using these methods. Each method is illustrated with code snippets and expected outputs to clarify their functionality.
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/ 3

https://developer.mozilla.

org/en-US/docs/Web/JavaScript/Reference/Global_Objects/
Array/slice

///////////// SPLICE /////////////////

const months = ['Jan', 'March', 'April', 'June'];


months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

const myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];


const removed = myFish.splice(2, 0, 'drum');
// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed

const myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];


const removed = myFish.splice(2, 0, 'drum', 'guitar');
// myFish is ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
// removed is [], no elements removed

const myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon'];


const removed = myFish.splice(2, 2);
// myFish is ["parrot", "anemone", "sturgeon"]
// removed is ["blue", "trumpet"]

const myFish = ['angel', 'clown', 'trumpet', 'sturgeon'];


const removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue');
// myFish is ["parrot", "anemone", "blue", "trumpet", "sturgeon"]
// removed is ["angel", "clown"]

const myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];


const removed = myFish.splice(2);
// myFish is ["angel", "clown"]
// removed is ["mandarin", "sturgeon"]

const arr = [1, , 3, 4, , 6];


console.log(arr.splice(1, 2)); // [empty, 3]
console.log(arr); // [1, 4, empty, 6]

///////////// PUSH /////////////////

const animals = ['pigs', 'goats', 'sheep'];


const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens', 'cats', 'dogs');


console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats",
"dogs"]

const sports = ['soccer', 'baseball'];


const total = sports.push('football', 'swimming');

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']


console.log(total); // 4

const vegetables = ['parsnip', 'potato'];


const moreVegs = ['celery', 'beetroot'];

// Merge the second array into the first one


vegetables.push(...moreVegs);

console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']

///////////// POP /////////////////

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];

console.log(plants.pop());
// expected output: "tomato"

console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]

plants.pop();
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]

const myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];


const popped = myFish.pop();
console.log(myFish); // ['angel', 'clown', 'mandarin' ]
console.log(popped); // 'sturgeon'

///////////// SLICE /////////////////

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

console.log(animals.slice());
// expected output: Array ["ant", "bison", "camel", "duck", "elephant"]

const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];


const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']

///////////// SPLIT /////////////////

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');


console.log(words[3]);
// expected output: "fox"

const chars = str.split('');


console.log(chars[8]);
// expected output: "k"

const strCopy = str.split();


console.log(strCopy);
// expected output: Array ["The quick brown fox jumps over the lazy dog."]

You might also like