-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrayindex.js
39 lines (30 loc) · 880 Bytes
/
arrayindex.js
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
// Accessing Array using index
const cities = ["FirstCity", "SecondCity", "ThirdCity", "LastCity"];
let firstcity = cities[0]; // Accessing the first item using its index
console.log(firstcity); // FirstCity
secondcity = cities[1];
console.log(secondcity); // SecondCity
let lastcity = cities[3];
console.log(lastcity); // ThirdCity
// Last index can be calculated as follows
let lastcity1 = cities.length - 1;
lastcity = cities[lastcity1];
console.log(lastcity); // LastCity
const countries = [
"Albania",
"Bolivia",
"Canada",
"Denmark",
"Ethiopia",
"Finland",
"Germany",
"Hungary",
"Ireland",
"Japan",
"Kenya",
]; // List of countries
console.log(countries); // -> all countries in array
console.log(countries[0]); // -> Albania
console.log(countries.length);
let lastIndex = countries.length - 1;
console.log(countries[lastIndex]); // -> Kenya