In this easy example, we have an array of colors, and we use shift() to remove the first color ("red") from the colors array. The removed color is stored in the removedColor variable, and the array is updated to contain only ["green", "blue"].
In this advanced example, we have a custom function processQueue that takes an array of tasks. It continuously processes tasks from the queue using shift() until the queue is empty. The function simulates advanced processing for each task.
When we call function with the array, which removes and processes tasks from the front of the queue until there are no tasks left. Showing how the shift() method can be used in more complex scenarios, such as managing task queues or processing data in a specific order.
In this easy example, we have an array of colors, and we use unshift("red") to add the color "red" to the beginning of the colors array. It returns the new length of the array and updates the array to be ["red", "green", "blue"].
In this advanced example, we have a custom function mergeArrays that takes two arrays and merges them by using the unshift() method. It iterates through second array in reverse order and adds its elements to the front of first array.
When we call mergeArrays with arrays as arguments it effectively merges the two arrays, resulting in merge one. This example show how the unshift() method can be used in more complex scenarios, such as combining arrays in a specific order.
In this easy example, we have two arrays, fruits1 and fruits2, which represent different sets of fruits. We use concat() to combine these arrays into one. As a result, we get a new array containing all the fruits from both original arrays.
In this advanced example, we have a custom function mergeArrays that accepts a variable number of arrays using the spread operator (...arrays). Inside the function, we use concat() to merge all the arrays into a single array and return the result.
When we call mergeArrays with list of arrays, it effectively combines the elements from all arrays into one. This example show how the concat() method can be used to merge an variety number of arrays together.
In this easy example, we have an array of numbers, and we use slice(1, 4) to extract a portion of the array, starting from index 1 ("2") and ending just before index 4 ("5"). It returns a new array, slicedNumbers, containing the selected elements. The original numbers array remains the same.
In this advanced example, we have a custom function getTopNItems that takes an array and a number n. It returns a new array containing the top n items from the input array. The function uses the slice() method to achieve this.
When we call getTopNItems(items, 3), it extracts the top three items from the items array and returns ["apple", "banana", "cherry"]. This example show how the slice() method can be used in more complex scenarios, such as retrieving a specific number of items from a list.
let say that we have an array of colors, and we use splice(2, 2) which mean that we gonna removes two elements starting from index 2 ("blue" and "yellow"). The removed elements are saved in variable removedColors, and the original colors array is updated.
another example we gonna create function insertElementAtIndex that takes an array (arr), an index (index), and an element (element). The function uses splice() to insert the specified element at the given index without remove any elements.
When we call insertElementAtIndex(numbers, 2, 3), it inserts the number "3" at index 2 in the numbers array. This show how the splice() method can be used for more complex operations, such as inserting elements at specific positions in an array.
Thank you for taking the time to explore the concepts of shift, unshift, concat, slice, and splice with me. I hope you found these examples and explanations helpful in understanding.
do not forget to like and subscribe, stay tuned for more videos and content. see you in the next one.