JavaScript Data Types And When To Use Them
Software Developer and Technical Writer
In JavaScript, data and its manipulation are two things we can't do without. In fact, JavaScript exists because of these data. Across all programming languages, there are many data structures like trees, graphs, hash and so on but some are built-in in some languages by default as in JavaScript with the case of Arrays, Sets, Maps and Objects. These data structures are the four built-in data structures in JavaScript and they have their advantages and use case scenarios over others. Apparently, we get data from three main sources in programming; from the source code eg status messages, from the User Interface, UI eg Input data in prompt message and from external sources like APIs. These data - depending on what we're going to use them for are saved in different data structures and as you can guess, in JavaScript, they include all four mentioned above. Now, here's the gist: if you're going to create a simple list, make use of Arrays or Sets. The distinction that comes with these two groups - Array and Sets or Maps and Objects - lie in the fact that the former does not use key-value pairs while the latter does. By key-value pairs, we mean, they won't be stored in a particular variable in that data structure. Now, you may have a problem choosing which to use between the first group (Arrays and Sets) and in that case, take these into note:
- Use Arrays when you need ordered list of values and that might contain duplicate values.
- Use Arrays when you are going to need to manipulate data easily too
- Use Sets when you need to work with unique values
- Use Sets when high-performance is necessary
- Use sets when you won't be needing duplicates.
As for the case of Maps and Objects, they make use of key-values i.e they have variables within them that describe the value(s) they hold. You might want to consider these rules if you want to use key-value data structures:
- Use objects when you're doing something quite easy; they're easier to write
- Use sets when high-performance is in question though
- When you need to use any data type as key, JavaScript maps are your go-to
- JavaScript maps are easier to compute and iterate over too
- Objects are more traditional and native to even beginners
- If you need to include functions or methods, objects are better.
- When you need to use JSON data, use Objects. You can convert to Maps later if you wish though.
- When you need to map keys to values, use maps
- Sometimes, we might have keys that are not strings. In cases like these, maps come very much in handy.
With these rules, you'd definitely find the right choice of data structure to use for whatever action you want to perform with your data. I have to mention too that JavaScript has two other not-really common data structures; WeakSets and WeakMaps. Other ones in general programming may include stacks, queues, hash tables, linked lists etc. With a wide opening of these, I hope you end up making good and impressive manipulations with your data no matter where you get them. Good luck!



