-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayPushPop.js
More file actions
48 lines (39 loc) · 2.34 KB
/
arrayPushPop.js
File metadata and controls
48 lines (39 loc) · 2.34 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
/*
// Array Push
var numbers = [78, 45, 98, 45];
console.log("Main array (numbers):", numbers);
// Output || Main array (numbers): [ 78, 45, 98, 45 ]
console.log("Length of the main array (numbers):", numbers.length);
// Output || Length of the main array (numbers): 4
// Adding a new element to the last of the main array (numbers) using 'push()' method
numbers.push(63);
console.log("The array (numbers) after adding a new element to the last:", numbers);
// Output || The array (numbers) after adding a new element to the last: [ 78, 45, 98, 45, 63 ]
console.log("New length of the array (numbers) after adding a new element to the last:", numbers.length);
// Output || New length of the array (numbers) after adding a new element to the last: 5
// We can also add more than one new element to the last of an array using 'push()' method
numbers.push(65, 66);
console.log("The array (numbers) after adding more than one new element to the last:", numbers);
// Output || The array (numbers) after adding more than one new element to the last: [ 78, 45, 98, 45, 63, 65, 66 ]
console.log("New length of the array (numbers) after adding more than one new element to the last:", numbers.length);
// Output || New length of the array (numbers) after adding more than one new element to the last: 7
*/
/*
// Array Pop
var numbers = [78, 45, 98, 45, 63, 65, 66];
console.log("Main array (numbers):", numbers);
// Output || Main array (numbers): [ 78, 45, 98, 45, 63, 65, 66 ]
console.log("Length of the main array (numbers):", numbers.length);
// Output || Length of the main array (numbers): 7
// Removing an element from the last of the main array (numbers) using 'pop()' method
numbers.pop();
console.log("The array (numbers) after removing an element from the last:", numbers);
// Output || The array (numbers) after removing an element from the last: [ 78, 45, 98, 45, 63, 65 ]
console.log("New length of the array (numbers) after removing an element from the last:", numbers.length);
// Output || New length of the array (numbers) after removing an element from the last: 6
// The array 'pop()' 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.pop();
console.log("Removed item as return:", itemOfNumbers);
// Output || Removed item as return: 65
*/