diff --git a/files/en-us/web/javascript/reference/global_objects/array/at/index.md b/files/en-us/web/javascript/reference/global_objects/array/at/index.md index d3c97d800245635..9cf1e17cce30fb7 100644 --- a/files/en-us/web/javascript/reference/global_objects/array/at/index.md +++ b/files/en-us/web/javascript/reference/global_objects/array/at/index.md @@ -32,6 +32,8 @@ The `at()` method is equivalent to the bracket notation when `index` is non-nega The usual practice is to access {{jsxref("Array/length", "length")}} and calculate the index from that — for example, `array[array.length - 1]`. The `at()` method allows relative indexing, so this can be shortened to `array.at(-1)`. +By combining `at()` with {{jsxref("Array/with", "with()")}}, you can both read and write (respectively) an array using negative indices. + The `at()` method is [generic](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#generic_array_methods). It only expects the `this` value to have a `length` property and integer-keyed properties. ## Examples @@ -110,5 +112,6 @@ console.log(Array.prototype.at.call(arrayLike, 2)); // undefined - {{jsxref("Array")}} - {{jsxref("Array.prototype.findIndex()")}} - {{jsxref("Array.prototype.indexOf()")}} +- {{jsxref("Array.prototype.with()")}} - {{jsxref("TypedArray.prototype.at()")}} - {{jsxref("String.prototype.at()")}} diff --git a/files/en-us/web/javascript/reference/global_objects/array/with/index.md b/files/en-us/web/javascript/reference/global_objects/array/with/index.md index 3585b073cfa00fd..be45d3f725f8bb6 100644 --- a/files/en-us/web/javascript/reference/global_objects/array/with/index.md +++ b/files/en-us/web/javascript/reference/global_objects/array/with/index.md @@ -37,6 +37,8 @@ A new array with the element at `index` replaced with `value`. The `with()` method changes the value of a given index in the array, returning a new array with the element at the given index replaced with the given value. The original array is not modified. This allows you to chain array methods while doing manipulations. +By combining `with()` with {{jsxref("Arra/at", "at()")}}, you can both write and read (respectively) an array using negative indices. + The `with()` method never produces a [sparse array](/en-US/docs/Web/JavaScript/Guide/Indexed_collections#sparse_arrays). If the source array is sparse, the empty slots will be replaced with `undefined` in the new array. The `with()` method is [generic](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#generic_array_methods). It only expects the `this` value to have a `length` property and integer-keyed properties. @@ -100,4 +102,5 @@ console.log(Array.prototype.with.call(arrayLike, 0, 1)); - {{jsxref("Array.prototype.toReversed()")}} - {{jsxref("Array.prototype.toSorted()")}} - {{jsxref("Array.prototype.toSpliced()")}} +- {{jsxref("Array.prototype.at()")}} - {{jsxref("TypedArray.prototype.with()")}} diff --git a/files/en-us/web/javascript/reference/global_objects/typedarray/at/index.md b/files/en-us/web/javascript/reference/global_objects/typedarray/at/index.md index ac6115e81a54f16..3c33fd7937c636f 100644 --- a/files/en-us/web/javascript/reference/global_objects/typedarray/at/index.md +++ b/files/en-us/web/javascript/reference/global_objects/typedarray/at/index.md @@ -84,5 +84,6 @@ console.log(atWay); // 11 - {{jsxref("TypedArray")}} - {{jsxref("TypedArray.prototype.findIndex()")}} - {{jsxref("TypedArray.prototype.indexOf()")}} +- {{jsxref("TypedArray.prototype.with()")}} - {{jsxref("Array.prototype.at()")}} - {{jsxref("String.prototype.at()")}} diff --git a/files/en-us/web/javascript/reference/global_objects/typedarray/with/index.md b/files/en-us/web/javascript/reference/global_objects/typedarray/with/index.md index 712f374186e6ac6..953b68da029bb79 100644 --- a/files/en-us/web/javascript/reference/global_objects/typedarray/with/index.md +++ b/files/en-us/web/javascript/reference/global_objects/typedarray/with/index.md @@ -59,4 +59,5 @@ console.log(arr); // Uint8Array [1, 2, 3, 4, 5] - [JavaScript typed arrays](/en-US/docs/Web/JavaScript/Guide/Typed_arrays) guide - {{jsxref("TypedArray.prototype.toReversed()")}} - {{jsxref("TypedArray.prototype.toSorted()")}} +- {{jsxref("TypedArray.prototype.at()")}} - {{jsxref("Array.prototype.with()")}}