[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/with [Back]  [Original]

Array.prototype.with() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.with()

Baseline

20237

with() Array

js
arrayInstance.with(index, value)

index

  • -array.length <= index < 0 index + array.length
  • RangeError
value

index value

RangeError

index >= array.length index < -array.length

with()

with() at()

with() undefined

with() this length

js
const arr = [1, 2, 3, 4, 5];
console.log(arr.with(2, 6)); // [1, 2, 6, 4, 5]
console.log(arr); // [1, 2, 3, 4, 5]

with()

js
const arr = [1, 2, 3, 4, 5];
console.log(arr.with(2, 6).map((x) => x ** 2)); // [1, 4, 36, 16, 25]

with()

with()

js
const arr = [1, , 3, 4, , 6];
console.log(arr.with(0, 2)); // [2, undefined, 3, 4, undefined, 6]

with()

with() this length length this index value

js
const arrayLike = {
  length: 3,
  unrelated: "foo",
  0: 5,
  2: 4,
  3: 3, // length  3  with() 
};
console.log(Array.prototype.with.call(arrayLike, 0, 1));
// [ 1, undefined, 4 ]

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.with


Web Proxy Viewer  |  New URL  |  Original Page