Difference between Array and a Linked List?


I know the differences but I have a problem to understand with 1 major difference. It is Array has fixed size and Linked List has dynamic size. I really don’t understand this as I can define an empty array like below

var arr = [ ];

And insert whenever required like normal push or like

    arr[0] = "apple";
    arr[1] = "banana";
    arr[2] = "custard"; 

and so on…
Infact when the length is unknown I can do like

    arr[arr.length] = "zuccini";

So I can change the size at runtime or declare an empty array and thus make it dynamic. So the claim that array has a disadvantage of fixed size as compared to linked list which has an dynamic size isn’t wrong? Please help me in clearing out this confusion. Thank you!


The thing is the array in javascript is not the same as an array in C or C++. In C for example an array is a piece of memory with consecutive addresses. In javascript an array is a C++ class (at least in a C++ interpreter) under the hood and it should behave like an array but under the hood it is implemented in it’s own way.


Sorry. What do you exactly mean by C++ class?


C++ is a programming language. A class is a piece of code that provides a blueprint for creating objects. These objects once instantiated by a class have an internal state that can be manipulated. Javascript is not directly compiled to machine code. It get’s read by another programm (in our case that programm is written in C++) that then interprets the language and executes it. So when you use an Array in javascript the interpreter (our C++ programm) creates a new object to hold your values and manages them in memory. Does that help?


Oh. This is really deep. So basically in ‘C’ and ‘C++’ arrays are fixed size but not in Java script as the interpreter creates a new object which in turn it makes a java script array dynamic. Correct me if am wrong.


That’s correct :slight_smile:


Wow. I realized something new and deep today. Happy to know it. Thanks mate for the help.


You’re welcome. For example google’s javascript engine that is in the chrome browser is called V8 and is written in C++. Firefox uses an engine called SpiderMonkey that is written in C.


Ok. I think I will have to research a bit more as I saw few posts showing declaring an empty array in C and C++


I see that there is something called as flexible array member which can be declared as an empty array


Many data structures cannot be truly understood or appreciated without talking about computer memory and how data is represented and stored in memory - every data structure has an abstract definition of use that may not refer to memory - this is more true of dynamic interpreted languages like javascript than compiled languages like C++ - but even for interpreted languages a certain depth of knowledge requires understanding how memory is used by the language

There is no native linked list in javascript so any discussion of linked lists in javascript is kind of pointless - we’d have to agree on some idea of a linked list possibly like https://beta.freecodecamp.com/en/challenges/coding-interview-data-structure-questions/work-with-nodes-in-a-linked-list

It is even more pointless if this kind of linked list is based on a javascript array or map or object because it offers no advantage over the native data structures - in fact it only adds overhead and rightly only confuses people

We could talk about linked lists and arrays in C++ - but it’s off-topic for this forum and there are many sites with details


There’s actually a bigger difference between just being dynamic vs static, and that’s in how you actually access the members. With an array, you can pick any arbitrary element, say, #3, by referencing it at that index. With a LL, you can’t, you must always iterate through the list and count, so to find the 3rd element, you have to start at the Head, then go to the next element, then you’ll get to the 3rd element.

Because of this, the way you actually add members to the middle of the array vs a linked list changes drastically. With an array, if you want to add something to the middle, you have to literally copy half of the entire array into a new location to make room for it, and if it’s a large array this requires a lot of work for the system. A linked list, however, you literally just have to tell two items that they’ve got a new neighbor and that’s it.

So, the trade off between an Array and a Linked List has way more to do with how you actually access and interact with the data than the size or memory. Please let me know if that helps or if you have any other questions!


Yes, how the memory is allocated is an important part of the difference, but another big one is how they are traditionally accessed.

An array can be accessed by index, but a linked list is traditionally accessed by inching through, node by node, like working your way around rosary beads (if it was a line instead of a circle.) Traditionally with a linked list, you don’t jump to an index.

And as people have pointed out, arrays in JS can dynamically allocate memory so a linked list becomes a little redundant. If you really need a linked list, you can just use an array and use it like a linked list. You can get all the functionality without having to code a new data structure.

That being said, I think there is some value in learning these different data structures: linked lists, queues, stacks, trees, etc. Even if you don’t use them or could use some library to implement them if you needed them, you learn a lot about data structures and algorithms as you do it. You could do it in any language, but if JS is your language, you could certainly do it there - even if it isn’t really needed.


Thats a very interesting point and a reason that makes me to scratch my head more and research now.

Only C++? Can Python also be a good option learn Data Structures?
Thank you!


Data structures can be studied in the abstract if that’s your interest - certainly there are textbooks for that - there may be some websites too

When learning a language like javascript that is interpreted and hides memory management from programs it is best to take advantage of the higher-level facilities provided by the language and build even more powerful abstractions

If you want to understand the role of memory in programming then C is the place to start - C++ is fine too but now has many data structures that eliminate or reduce the need to deal with memory management - this is good for programming but makes it harder to see what’s happening under the covers


I actually disagree with @ppc, I think learning Data Structures would still be valuable for anyone learning Javascript just the same. Yes, you won’t get nearly as much out of them as you might in a language like C++, but it’s still extremely useful to know how things work and especially how they solve problems. Just keep in mind that JavaScript only has a handful of base data structures you can use. Arrays, Sets, Maps and Dictionaries.

I do think Python might see more benefit than Javascript from Data Structures.


I’m not sure what I said is being disagreed with by @HeinousTugboat - of course data structures are essential for programmers - but it does not make sense to design or understand a data structure so intricately tied to memory allocation in a language that does not allow any control over memory - one could say “linked lists in javascript! - that’s fake news - it’s sad!”

it’s important for programmers to know the tools they are working with and leverage their power

nobody can stop you from smashing a hammer on a power drill to punch a hole in a wall at home but just try doing that on a construction site …


My apologies, I got the impression you were suggesting that discussing data structures in Javascript was kind of pointless, had no advantages, and that we should instead use the higher-level facilities included in Javascript.