#JS with() new Array method

The with method is a new addition to JavaScript's array methods introduced in ECMAScript 2023 (ES14). It allows you to create a new array with a value replaced at a specific index, without modifying the original array.

to solve this problem of mutability and achieve immutability while changing an element of an array.

The with() method takes two parameters:

  • index - An index value which can start from 0, and also can be a negative number. The negative index counts backwards from the end of the array.
  • value - The value to change at a given index.

Here's the syntax and an example of how to use the with method:

let originalArray = [1, 2, 3, 4, 5];
let newArray = originalArray.with(2, 99); // replaces the value at index 2 with 99

console.log(originalArray); // [1, 2, 3, 4, 5]
console.log(newArray); // [1, 2, 99, 4, 5]        

In this example:

  • originalArray is the original array.
  • originalArray.with(2, 99) creates a new array where the element at index 2 (which is 3) is replaced with 99.
  • The original array remains unchanged, demonstrating the immutability of the operation.

As the with() method returns an array, you can chain the output of the with() method along with other array methods, like map(), filter(), reduce(), some(), every() etc.

要查看或添加评论,请登录

Asad Khan的更多文章

  • Next.js 15 has FINALLY arrived!

    Next.js 15 has FINALLY arrived!

    Next.js 15 is now officially stable and ready for production! This release incorporates enhancements from both RC1 and…

  • Error Cause: Enhanced Debugging

    Error Cause: Enhanced Debugging

    In ES13 (ECMAScript 2022), the property was introduced for the object. This property allows you to chain errors…

  • #JS Dynamic Object Keys

    #JS Dynamic Object Keys

    This one-liner uses computed property names, where the value of the prop variable is used as the key name inside the…

  • Object property existence check in JS

    Object property existence check in JS

    Suppose building a user profile system and need to check if a specific attribute (like "email") is present in a user…

  • Difference between return vs return await #JavaScript

    Difference between return vs return await #JavaScript

    In JavaScript, when using functions and to handle asynchronous operations, there's a difference between and . Return:…

  • Next.js 15 Release Candidate (RC) is now available

    Next.js 15 Release Candidate (RC) is now available

    The Next.js 15 Release Candidate (RC) is now available, offering a chance to test new features before the stable…

  • Angular v18 is now available!

    Angular v18 is now available!

    This version continues to innovate while bringing stability to the Angular ecosystem, which has become somewhat…

  • New upcoming array method called groupBy()

    New upcoming array method called groupBy()

    The method is a forthcoming addition to the JavaScript Array object, currently positioned at Stage 3 within the TC39…

  • Closure in JavaScript

    Closure in JavaScript

    In JavaScript, a closure is a combination of a function and the lexical environment within which that function was…

  • Lazy Loading

    Lazy Loading

    Lazy loading is a technique in web development where assets (such as images, scripts, or stylesheets) are loaded only…

社区洞察

其他会员也浏览了