var index = items.indexOf(3452);

if (index !== -1) {
    items[index] = 1010;
}

Also it is recommend you not use the constructor method to initialize your arrays. Instead, use the literal syntax:

var items = [523, 3452, 334, 31, 5346];

You can also use the ~ operator if you are into terse JavaScript and want to shorten the -1 comparison:

var index = items.indexOf(3452);

if (~index) {
    items[index] = 1010;
}

Sometimes I even like to write a contains function to abstract this check and make it easier to understand what's going on. What's awesome is this works on arrays and strings both:

var contains = function (haystack, needle) {
    return !!~haystack.indexOf(needle);
};

// can be used like so now:
if (contains(items, 3452)) {
    // do something else...
}

Starting with ES6/ES2015 for strings, and proposed for ES2016 for arrays, you can more easily determine if a source contains another value:

if (haystack.includes(needle)) {
    // do your thing
}
Answer from Eli on Stack Overflow
๐ŸŒ
Medium
tofusoup429.medium.com โ€บ javascript-replace-elements-in-array-with-one-line-of-code-8eee252a66ff
[JavaScript]Replace elements in Array with one line of code | by Steve Kim | Medium
August 12, 2021 - We often need to replace an element or elements in an Array. There are no explicitly named method, โ€œreplaceโ€, in JavaScript.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-replace-an-item-from-an-array-in-javascript
JavaScript - Replace an Item from JS Array - GeeksforGeeks
July 12, 2025 - The Array Indexing approach replaces an item in an array by directly accessing its position using the index and assigning a new value. This method is straightforward, modifies the original array, and is ideal for known index positions.
๐ŸŒ
DEV Community
dev.to โ€บ albertomontalesi โ€บ find-and-replace-elements-in-array-with-javascript-2e4n
Find and Replace elements in Array with JavaScript - DEV Community
May 10, 2021 - These methods are useful because they can be used to both checks if an element exists in the Array while at the same time getting a reference as to where that element is positioned, which we can use to then replace that said element.
Find elsewhere
๐ŸŒ
CodingNomads
codingnomads.com โ€บ javascript-array-splice-insert-delete-replace
How to Replace, Add or Delete Array Items: JavaScript Splice
Unlimited access to beginner-to-professional courses in Java, Python, JavaScript, Data Science, ML, Web Dev, and more ยท Hands-on practice with IDEs, quizzes, labs, challenges, and thousands of interactive lessons ยท Professional credibility with shareable completion certificates (like this) ... .splice() is versatile, allowing addition, deletion, and replacement of array elements in a single operation.
๐ŸŒ
WeWeb Community
community.weweb.io โ€บ ask us anything โ€บ how do i?
How do I replace a specific item in an array - How do I? - WeWeb Community
September 27, 2024 - Lets say I have 9 images in an array and I want to replace image 5 with a different image. Does anyone know how I would go about that or if its possible? If there is a tutorial of some kind I would appreciate it because I am a bit lost here. For more detail I am using file upload advanced, ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ replace
String.prototype.replace() - JavaScript | MDN
However, this replacer would be hard to generalize if we want it to work with any regex pattern. The replacer is variadic โ€” the number of arguments it receives depends on the number of capturing groups present. We can use rest parameters, but it would also collect offset, string, etc. into the array.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ curriculum help
How do I replace multiple strings in an array of Items - Curriculum Help - The freeCodeCamp Forum
January 16, 2021 - I have this code; let myArray = ['he123llo', 'cats', 'wor123ld', 'dogs']; const result = myArray.map(x =>x .replace('1', '') .replace('2', '') .replace('3', '') .replace('s', '') ); console.log(result); //[ 'hello', 'cat', 'world', ...
๐ŸŒ
PayPal
developer.paypal.com โ€บ docs โ€บ api โ€บ orders โ€บ v2
Orders
Array ([ 0 .. 32767 ] items) ... Request is not well-formed, syntactically incorrect, or violates schema. ... The requested action could not be performed, semantically incorrect, or failed business validation. ... Sample 5 - 422 - Patch Order - 422 Unprocessable Entity Error - Payer Patch Not AllowedSample 1 - 204 - Patch Order - Add Shipping Address ... Sample 3 - 204 - Patch Order - Add Items and Replace ...
๐ŸŒ
GitHub
github.com โ€บ zloirock โ€บ core-js
GitHub - zloirock/core-js: Standard Library ยท GitHub
Standard Library. Contribute to zloirock/core-js development by creating an account on GitHub.
Author ย  zloirock
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ replace-or-append-array-value-28595
JavaScript Array Manipulation | Programming Exercises | LabEx
Learn how to replace or append values in a JavaScript array using the spread operator, Array.prototype.findIndex(), Array.prototype.push(), and Array.prototype.splice().
๐ŸŒ
JSchallenger
jschallenger.com โ€บ javascript-basics โ€บ arrays โ€บ replace-array-element-at-index
Replace Array Element at Index | JSchallenger
Home JavaScript Basics arrays ยท Replace Array Element at Index ยท You are given an array and must replace the element at a specific index with a new value. Complete the function so that it returns the updated array. Premium ยท reset ยท JavaScript ยท
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 8 )
6 days ago - The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab". ... Splits this string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.
๐ŸŒ
Laravel
laravel.com โ€บ docs โ€บ 12.x โ€บ helpers
Helpers | Laravel 12.x - The clean stack for Artisans and agents
Laravel is a PHP web application framework with expressive, elegant syntax. We've already laid the foundation โ€” freeing you to create without sweating the small things.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Replacing the elements of an array with elements from another array - JavaScript - The freeCodeCamp Forum
September 10, 2019 - I have an array say var array1=[โ€œaโ€, โ€œdโ€, โ€œaโ€, โ€œfโ€]; var array2=[โ€œ1โ€, โ€œ2โ€, โ€œ3โ€, โ€œ4โ€] I want my output to be like so: [โ€œ1โ€, โ€œ2โ€, โ€œ1โ€, "3] this is what I have so far: function encoderSolution( raw, codeWords) { var โ€ฆ