Let's start with a quote that once changed my life. I am sure that this
quote and its understanding will also change yours if you have not heard it
Bad programmers worry about the code. Good programmers worry about data
structures and their relationships.[Linus Torvalds]
We will talk about data structures, but first, it is impossible to talk
about them without mentioning algorithms.
Algorithms are the steps we need to take to solve a problem. Data
structures are organized data with efficient and convenient access to
Programming is always = algorithms ➕ data structures.
When solving a problem, it should be kept in mind that choosing the right
data structure may be a solution for the problem (no need to write any
Data structures in JS
JS has two basic data-structures that can be reused in different ways
depending on the requirements. Therefore, they support a large number of
methods of different structures which would be separate in other program
Object (Enum, Map, Graph, etc);
Array (List, Stack, Queue, etc).
Let's get started!
We will use this user type for further examples:
Do not worry if you are not familiar with TypeScript. The example of the type
is given so that you understand the approximate structure of the user.
Let's imagine that we have a task — we need to display a list of users using
the appropriate emoji by the user's gender. For now we have only two options —
male and female.
It might look like this:
That's it. The task is done. Buuut... we can do it better.
Let's imagine that in our code there will be or there already are places where
we check something by the user's gender. We duplicate the code. It is not
good. Also if we use *just a string* all the time, sooner or later, we will
make a mistake. In fact, such errors are not very easy to find.
Let's solve this task too. We will protect ourselves from this. We will use a
single source of truth — CONSTANT's.
CONSTANT s are used to describe data that is known *before the start* of the
program and that *should not be changed* during the execution of the program.
When associated with an identifier, a constant is said to be "named,"
although the terms "constant" and "named constant" are often used
The constant can be declared with any keyword for variables (var, let or
const), but const is commonly used.
Constants are a very important and popular approach to organize program code.
And there are several conventions among developers for them:
Constant must be declared at the top of the program/module (after imports, if any);
Constant must have a name in capital letters;
Constant must not be redefined anywhere in the program.
But it looks very ugly. We could replace this with a
switch statement, or a helper
function that does it itself. This is a significant improvement, but we can
also use special structures for this. We can use a Map data structure to make the solution more flexible and readable.
Map (dictionary, associative
array, map) — a data structure that is used to map one value to
JS has a new Mapconstructor out of the box. The key difference from a common object is the ability to
use any data type (even an object) as a key.
Usually, using the JS Map constructor is overkill. If you need the
functionality that the JS Map provides, you have to use it. But usually a
common object is used to imitate this structure.
There are some conventions how to configure this structure:
Maps must have a name by one of these patterns —
or userMap). The first
pattern is used more often.
Usually, the solution that the language itself can offer is many times more
effective than any third-party library can offer.
Data structures are awesome! They help us solve tasks in a neat and
convenient way. With the right data structures, it's easier to build
algorithms or not write code at all. The data structure may already be the
solution to the task.
A huge plus is that if you do them according to the conventions that are
code much faster than coming up with something 'new.'
Most things are already invented for us!
Do not forget to put the constants of the same type into
Enum s, then map them into
the format you need, and enjoy the beauty of the data structures.
Enjoyed this article?
you might want to subscribe for our newsletter to get more content like this: