-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayUnshiftShift.js
More file actions
49 lines (39 loc) · 2.32 KB
/
arrayUnshiftShift.js
File metadata and controls
49 lines (39 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
// Array Unshift
var numbers = [6, 8, 10];
console.log("Main array (numbers):", numbers);
// Output || Main array (numbers): [ 6, 8, 10 ]
console.log("Length of the main array (numbers):", numbers.length);
// Output || Length of the main array (numbers): 3
// Adding a new element to the first of the main array (numbers) using 'unshift()' method
numbers.unshift(2);
console.log("The array (numbers) after adding a new element to the first:", numbers);
// Output || The array (numbers) after adding a new element to the first: [ 2, 6, 8, 10 ]
console.log("New length of the array (numbers) after adding a new element to the first:", numbers.length);
// Output || New length of the array (numbers) after adding a new element to the first: 4
// We can also add more than one new element to the first of an array using 'unshift()' method
numbers.unshift(4, 5);
console.log("The array (numbers) after adding more than one new element to the first:", numbers);
// Output || The array (numbers) after adding more than one new element to the first: [ 4, 5, 2, 6, 8, 10 ]
console.log("New length of the array (numbers) after adding more than one new element to the first:", numbers.length);
// Output || New length of the array (numbers) after adding more than one new element to the first: 6
*/
/*
// Array Shift
var numbers = [2, 4, 6, 8, 10];
console.log("Main array (numbers):", numbers);
// Output || Main array (numbers): [ 2, 4, 6, 8, 10 ]
console.log("Length of the main array (numbers):", numbers.length);
// Output || Length of the main array (numbers): 5
// Removing an element from the first of the main array (numbers) using 'shift()' method
numbers.shift();
console.log("The array (numbers) after removing an element from the first:", numbers);
// Output || The array (numbers) after removing an element from the first: [ 4, 6, 8, 10 ]
console.log("New length of the array (numbers) after removing an element from the first:", numbers.length);
// Output || New length of the array (numbers) after removing an element from the first: 4
// The array 'shift()' method returns us the removed element from the array, which we can use later anywhere by storing it inside a variable.
var numbersNew = [78, 45, 63, 65];
var itemOfNumbers = numbersNew.shift();
console.log("Removed item as return:", itemOfNumbers);
// Output || Removed item as return: 78
*/