DEV Community

Morris John
Morris John

Posted on

Why should one use multiple Variables when you could store everything in an array?

So I just started learning JavaScript (using Angela Yu's Udemy course) and I'm presently learning arrays. So I'd like to know why one would use variables when you could store all the data you need in one array and use the .push to always add since it looks as though JavaScript doesn't have size limit for arrays.

Thanks

Top comments (6)

Collapse
 
terabytetiger profile image
Tyler V. (he/him)

Your mom tells you you have to clean your room. You have 2 options:

a) Throw everything into your closet, close the door and when you need something you can find it by digging through and looking for it (using a single array for everything)

b) Take the time to sort everything into their proper location, making it easier to find if you need a specific thing (multiple variables).

The quick and dirty way is to throw things into an array, but as you .push things to the array, you have to keep track of what is pushed when, otherwise you won't know how to reference the values later. If you sort things into the appropriate variables, they'll be much easier to reference later on!

Collapse
 
morrisjohn profile image
Morris John

Thanks. I loved the room cleaning analogy

Collapse
 
tobiassn profile image
Tobias SN

Because with variables you can have actual names, but with an array you only have numbers, and if you remove one element, it pushes everything else to the left, which makes keeping track of stuff difficult. If you just ignore elements you’re not using, you’ll end up wasting a lot of memory. Really, you’re just making yourself deal with a lot of problems that variables don’t have.

Collapse
 
morrisjohn profile image
Morris John

Thank you very much.

Collapse
 
technoplato profile image
Michael Lustig - halfjew22@gmail.com

Hey Morris, I'm loving Tyler's analogy about cleaning your room.

Another approach I find helpful before I've really developed a deep understanding of a concept is to just try out what I'm thinking. I find it's the best way to learn. Make the mistakes yourself, and the lessons will stick around in your memory much longer than if someone explains them to you.

So, give arrays a try. Try creating one array per program and see how it feels and works for you.

Perhaps even look into "functional" programming where (WARNING: HAND WAVY EXPLANATION UPCOMING) the program runs as an "array" of commands one by one.

Write back in a week with some of your thoughts about just using arrays. It's definitely an interesting idea, and isn't too far from what the computer is doing underneath the covers. Most would likely just find it easier to take the time and sort everything into its proper spot, but you could be different!

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

Readable vs Writable.

When you save something into the most concise container possible, you have optimized it for writing. You then pay the cost later of interpreting it. Fast forward a year, and someone else is reading the code. They have to reverse engineer what each piece is meant to represent based on its usage.

If you label what each piece means and use them in commonly-understood ways, there is less work for the future reader of the code to gain understanding of what the code does. However, it may also mean there are more lines to change when the logic needs to change. It is optimized for reading, not writing.

The skill that comes with experience in programming is how to strike the right balance (or which side to lean toward) in a given situation.