Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript array negative index

const arr = ["first", "second", "third"]
console.log(arr[-1]) // Will return undefined
Comment

negative index javascript

// Usually negative indexes do not work

const array = [ 1, 2, 3 ];
const string = "123";

console.log(array[-1]); // -> undefined
console.log(string[-1]); // -> undefined

// However arrays and strings have an `at` property which allows negative indexes
// a negative index will work from the end of the array/string backwards:
Indexes: 0  1  2
Array: [ 1, 2, 3 ]
Indexes:-3 -2 -1

/* Array/String.prototype.at essentially acts like this:
Array.prototype.at = String.prototype.at = function(index) {
	if (index < 0) {
		index = -index;
		while (index >= this.length) {
			index -= this.length;
		}
		if (index === 0) {
			return this[0];
		}
		return this[this.length - index];
	} else {
		while (index >= this.length) {
			index -= this.length;
		}
		return this[index];
	}
};
*/

console.log(array.at(-1)); // -> 3
console.log(string.at(-1)); // -> 3
Comment

javascript array negative index

const arr = ["first", "second", "third"]
console.log(arr[-1]) // Will return undefined
Comment

negative index javascript

// Usually negative indexes do not work

const array = [ 1, 2, 3 ];
const string = "123";

console.log(array[-1]); // -> undefined
console.log(string[-1]); // -> undefined

// However arrays and strings have an `at` property which allows negative indexes
// a negative index will work from the end of the array/string backwards:
Indexes: 0  1  2
Array: [ 1, 2, 3 ]
Indexes:-3 -2 -1

/* Array/String.prototype.at essentially acts like this:
Array.prototype.at = String.prototype.at = function(index) {
	if (index < 0) {
		index = -index;
		while (index >= this.length) {
			index -= this.length;
		}
		if (index === 0) {
			return this[0];
		}
		return this[this.length - index];
	} else {
		while (index >= this.length) {
			index -= this.length;
		}
		return this[index];
	}
};
*/

console.log(array.at(-1)); // -> 3
console.log(string.at(-1)); // -> 3
Comment

PREVIOUS NEXT
Code Example
Javascript :: typeof regex 
Javascript :: If Presence Shorthand javascript 
Javascript :: template.json exlude files from generating 
Javascript :: Array.find Shorthand javascript 
Javascript :: express plus es5 
Javascript :: taylors javascript test 
Javascript :: vs code javascript type check 
Javascript :: load widget after read data from json file 
Javascript :: simple promise 
Javascript :: second level relationships data not found in strapi 
Javascript :: on scroll image blur jquery 
Javascript :: convert online code javascript to python 
Javascript :: mongoose connecting directly rather than tunnel 
Javascript :: gitignore jsconfig 
Javascript :: Finding the longest word in a string 
Javascript :: display js variable in html without + 
Javascript :: unable to append div multiple times 
Javascript :: javascript grow function 
Javascript :: route methods 
Javascript :: nodejs post req accept form data 
Javascript :: change teh value of a slider p5js 
Javascript :: alertify.js styled success messae 
Javascript :: react native update performance useReducer 
Javascript :: Pass Props to a Component Using Short circuit evaluation in react 
Javascript :: js date add days daylight saving 
Javascript :: url is not a constructor javascript 
Javascript :: angular cache interceptor 
Javascript :: js plugin for drop down with images 
Javascript :: x is not a function javascript type error 
Javascript :: create 24 hours array like 00:00 to 23:30 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =