javascript get the last element of an array
var colors = ["red","blue","green"];
var green = colors[colors.length - 1]; //get last item in the array
let array = [1,2,3,4,5];
let lastElement = array.pop();
// array -> [1,2,3,4];
// lastElement = 5;var colors = ["red","blue","green"];
var green = colors[colors.length - 1];//get last item in the arrayif (loc_array[loc_array.length - 1] === 'index.html') {
// do something
} else {
// something else
}let arr = ['a', 'b', 'c']
arr.slice(-1)[0] // returns last element in an array
arr.pop()//the last one
Also in JavaScript:
- math js
- json-server localhost
- nodejs map
- display array in div javascript
- javascript return value from async function site:stackoverflow.com
- javascript eval passing variable
- access session data from ejs view
- check browser locale javascript
- javascript how to check if image exists
- lexical scope javascript
- javascript try catch finally
- discord.js bad word filter
- iterate object js
- jquery selector this and class
- useReactiveVar
- js toFixed
- remove first 3 characters from string javascript
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project electronicbookshop: Compilation failure
- multiple image upload with preview and delete react js
- JavaScript append HTML
- js require is not defined
- remove letter js
- react native init project
- ssr in angular 9 example