1var colors = ["red","blue","green"];
2var green = colors[colors.length - 1]; //get last item in the array
3
1const str = "Users/Edit/12345";
2let idx = str.lastIndexOf("/"); // returns 10
3idx = str.lastIndexOf("x"); // returns -1
4
5idx = str.lastIndexOf("Edit") // returns 6
6idx = str.lastIndexOf("edit") // returns -1 -- case sensitive
7
8// add "from" parameter
9// the search happens backwards, starting at the provided from parameter
10idx = str.lastIndexOf("s"); // returns 4
11idx = str.lastIndexOf("s", 3); // returns 1
12
1
2var str = "Please locate where 'locate' occurs!";
3
4var ind1 = str.indexOf("locate"); // return location of first value which founded
5var ind2 = str.lastIndexOf("locate"); // return location of last value which founded
6var ind3 = str.indexOf("locate", 15); // start search from location 15 and then take first value which founded
7var ind4 = str.search("locate");
8//The search() method cannot take a second start position argument.
9//The indexOf() method cannot take powerful search values (regular expressions).
10
11document.write("<br>" + "Length of string:", len);
12document.write("<br>" + "indexOf:", ind1);
13document.write("<br>" + "index of last:", ind2);
14document.write("<br>" + "indexOf with start point:", ind3);
15document.write("<br>" + "search:", ind4);
1'canal'.lastIndexOf('a'); // retorna 3
2'canal'.lastIndexOf('a', 2); // retorna 1
3'canal'.lastIndexOf('a', 0); // retorna -1
4'canal'.lastIndexOf('x'); // retorna -1
5'canal'.lastIndexOf('c', -5); // retorna 0
6'canal'.lastIndexOf('c', 0); // retorna 0
7'canal'.lastIndexOf(''); // retorna 5
8'canal'.lastIndexOf('', 2); // retorna 2
9
1var numbers = [2, 5, 9, 2];
2numbers.lastIndexOf(2); // 3
3numbers.lastIndexOf(7); // -1
4numbers.lastIndexOf(2, 3); // 3
5numbers.lastIndexOf(2, 2); // 0
6numbers.lastIndexOf(2, -2); // 0
7numbers.lastIndexOf(2, -1); // 3