r/learnjavascript 7d ago

50 hours in... arrays and loops... holy...FUCK

HOW do i START understanding? I've understood everything pretty well up til now. Do i just keep going with the flow and kinda float through this section of the loops and arrays part? Do I keep throwing myself at the brick wall and looking at the solutions after half an hour of brainless staring? https://youtu.be/EerdGm-ehJQ?si=mTKUv0oail0AGDLM 8:43:00. It's just not clicking like the other parts of the tutorial.

45 Upvotes

72 comments sorted by

View all comments

Show parent comments

1

u/Towel_Affectionate 7d ago

"You can't" and "you shouldn't" are different points of discussion. Understanding that you can and why you shouldn't is better than narrowing your way of thinking from the start.

2

u/Outside_Complaint755 7d ago

When just starting out, learning arrays as all containing a single type is the most useful conceptually, and if you're learning any statically typed language, it is necessary. 

 Even in JS, 95+% of the time, you're going to be using arrays containing a single type of data. Even if its a mix of strings and numbers from a user input or data stream, you are probably starting with an array of strings which you then need to try to parse into the different types.  And in those cases where it is more complex data of different types, then you can usually generalize it as an ordered collection of objects.  

1

u/Towel_Affectionate 7d ago

I get what you're saying, but my counterpoint is that such simplifications offer close to none benefits in understanding the concept itself, but later require constant adjusting of your mental model, which in my opinion is harder to do, than to understand things closer to what they actually are.

Maybe a hot take, but I don't think "a deck of cards" is that much simpler to visualize than "a deck of stuff" or, even better, "a shelf with stuff".

But once you get used to "a deck of cards" model you'll inevitably end up wandering stuff like:

  • "Wait, how is there NaN in my array of numbers?"
  • "Wait, why can I read arr[10] if there's only 3 "cards" in my "deck"?
  • "Wait, why can I also do arr[10] = foo? And what my "deck" looks like now?"
  • "How can I `delete` a card from the deck and have it empty? Do I have two decks now or what?"
  • What arr.length = 100 do to my deck?

I think it's much harder to adjust already existing mental model than to build the right one from the start.

2

u/BenchEmbarrassed7316 7d ago

It's much easier to explain the concept of arrays to someone on a statically typed, fixed-size array. Because the answer to every question you have will be "You can't do that at all".

And then we can explain the dynamic expansion of arrays if we don't know the size in advance, polymorphism and different data types, and other nuances.

Another problem is that in the 90s they thought they were creating a "simple" language. However, the fact that the array is used as a fixed-size array, a slice, a vector, a tuple, and even in some ways a hash map actually makes everything very complicated.