Resetting and managing JavaScript arrays created with 'new' keyword

I’m working on a Wordle helper app in JavaScript. It’s like those online solvers but works offline. I’ve got two main arrays:

  1. greenArray: Holds single letter guesses for each position.
let greenArray = new Array(5).fill(0);

How do I reset this to all zeros without wasting memory?

  1. yellowArray: Stores multiple letter guesses per position.
let yellowArray = Array(5).fill().map(() => []);

Can I just do yellowArray[i] = [] to reset each subarray?

Also, does reloading the page automatically free up memory from arrays created with new?

I’m new to JavaScript memory management and want to make sure I’m doing things right. Any tips would be awesome!

Great question about array management in JavaScript! For your greenArray, you can indeed reset it to all zeros efficiently by using the fill() method again:

greenArray.fill(0);

This will overwrite all elements with 0 without creating a new array.

As for yellowArray, your approach of yellowArray[i] = [] for each index is correct and memory-efficient. It replaces each subarray with a new empty array.

Regarding memory management, when you reload the page, the JavaScript engine automatically cleans up all variables and arrays, including those created with new. You don’t need to manually free memory in most cases.

One tip: consider using const for arrays you don’t plan to reassign. It doesn’t prevent modifying array contents but can help prevent accidental reassignments.

As someone who’s built a few word game helpers myself, I can share some insights. For greenArray, using fill(0) is indeed the most efficient way to reset it. It’s fast and doesn’t create unnecessary garbage.

For yellowArray, your intuition is correct. Using yellowArray[i] = for each position works well. It’s clean and doesn’t leave any lingering references.

About memory management, don’t sweat it too much. JavaScript’s garbage collector is pretty smart. When you reload the page, it’ll clean up everything, including arrays made with ‘new’.

One tip from my experience: if performance becomes an issue, consider using a single array with bit manipulation for green/yellow states. It’s more complex but can be blazing fast for larger dictionaries. Keep iterating on your app - it’s a great way to learn!

hey there! for greenArray, u can just do greenArray.fill(0) to reset it quick. yellowArray[i] = works fine too for each subarray. dont worry bout memory when reloading, javascript handles that for ya. keep it up with ur wordle helper, sounds cool!