🌐 AI搜索 & 代理 主页
Skip to content

Commit c9e9410

Browse files
authored
Arrays in JavaScript
1 parent 0129b29 commit c9e9410

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

arrays.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Arrays in JavaScript
2+
let arr1 = [2, 3, 1, 56, 21, 32, 100, 55, 45, 34, 43];
3+
// let arr2 = new Array(5);
4+
let arr2 = ["new", "old", "oldest", "latest"];
5+
6+
// Length of an array
7+
console.log("Length of array 1 is " + arr1.length);
8+
console.log("Length of array 2 is " + arr2.length);
9+
10+
// Searching an element
11+
console.log(arr1.indexOf(56));
12+
13+
// Sorting an array
14+
// Sorting in Ascending order
15+
let sortedArray = arr1.sort((a, b) => a - b);
16+
console.log(sortedArray);
17+
// Sorting in Desceding order
18+
let sortedArray2 = arr1.sort((a, b) => b - a);
19+
console.log(sortedArray2);
20+
21+
// Reversing an array
22+
let reversedArray = arr1.reverse();
23+
console.log(reversedArray);
24+
25+
// Concatenating two arrays
26+
let concatenatedArray = arr1.concat(arr2);
27+
console.log(concatenatedArray);
28+
29+
// Inserting an element in an array
30+
// Inserting an element at the end of an array
31+
let newArr = arr2.push("Fruits");
32+
console.log(arr2);
33+
// Inserting at the beginning of an array
34+
let newArr1 = arr1.unshift("Fruits");
35+
console.log(arr1);
36+
37+
// Deleting an element from an array
38+
// Deleting from last
39+
let newArr3 = arr2.pop();
40+
console.log(arr2);
41+
// Deleting from beginning
42+
let newArr4 = arr1.shift();
43+
console.log(arr1);
44+
45+
// Splicing in an array
46+
let splicedArray = arr1.splice(56);
47+
console.log(arr1);

0 commit comments

Comments
 (0)