Back to blog

Data Structures in Programming and JavaScript

Hello HaWkers! Jeff Bruchado here.

This is another article in our series that will cover our journey to Learn Programming from Scratch.

Today will be an article about another extremely important content to proceed with your learning.

So please! If you don't have mastery over what I'll talk about today, read to the end!

Well, without further ado, let's get started!

Today, we're going to talk about a super interesting topic: Data Structures.

To conceptualize, data structures are sets of data organized to facilitate their manipulation and use.

They are fundamental for solving many problems, as they allow storing and managing data efficiently.

Programming itself, and JavaScript, have some native data structures, which are integrated into the language. Different languages may have different structures, but the basic structures generally repeat and can be used directly.

These basic structures are the following:

Array

Array is a data structure that allows storing a collection of elements, which can be of any type.

It's possible to access array elements through an index, which is an integer that indicates the element's position in the array.

For example:

const numbers = [1, 2, 3, 4, 5];

console.log(numbers[0]); // 1 (first element of the array)
console.log(numbers[2]); // 3 (third element of the array)

PS: Remember that any code I exemplify here, I encourage you to open your browser console, copy it, and see the result of the operation.

Remember that to truly Learn Programming, you need to get your hands dirty, it's not enough to consume only theoretical content.

You need to create your own practical solutions to solidify the new knowledge in your memory.

Given this super important message, let's continue!

String

String is a data structure that represents a sequence of characters.

This is probably the easiest type to understand, because it only stores text.

Strings are delimited by single or double quotes and can be manipulated as if they were arrays, with access to each character through an index.

For example:

const name = 'Jeff';

console.log(name[0]); // "J" (first character of the string)
console.log(name[2]); // "f" (third character of the string)
console.log(name); // "Jeff"

Object

Objects are a data structure that allows storing key-value pairs.

Keys are strings or symbols that represent the property name, while values can be of any type.

Objects are delimited by braces { } and properties are separated by commas.

For example:

const user = {
  name: 'Jeff',
  alive: true,
};

console.log(user.name); // "Jeff" (value of the "name" property)
console.log(user.alive); // true (value of the "alive" property)

In addition to these native data structures, JavaScript also has some data structures that can be created from classes or constructors.

These structures are the following:

Map

Map is a data structure that allows storing key-value pairs, but unlike objects, keys can be of any type.

It's created from the Map class and is very useful when we need to store key-value pairs with keys that are not strings.

For example:

const phones = new Map();

phones.set(123456789, 'Jeff');
phones.set(987654321, 'Mary');

console.log(phones.get(123456789)); // "Jeff" (value associated with key 123456789)

Set

Set is a data structure that stores a set of unique elements, without worrying about the order in which elements were added.

It's created from the Set class and is very useful when we need to store elements without worrying about duplicates.

For example:

const set = new Set();

set.add(1);
set.add(2);
set.add(3);
set.add(1); // won't be added, as element 1 already exists in the set

console.log(set.has(1)); // true (element 1 belongs to the set)
console.log(set.has(4)); // false (element 4 doesn't belong to the set)

Linked List

Linked List is a data structure that consists of a sequence of nodes, where each node stores an element and a reference to the next node in the list.

It's useful when we need to store a large number of elements and need fast insertion and removal operations.

class Node {
  constructor(element) {
    this.element = element;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.size = 0;
    this.first = null;
    this.last = null;
  }
  // other linked list methods, like insertion, removal, etc.
  // ...
}

These are just some of the main data structures present in Programming and JavaScript.

There are many others, like stacks, queues, trees, graphs, etc., each with its own characteristics and applications.

But in my humble opinion, you don't have a deep need to delve into all these types.

At least not for now.

Conclusion

Well HaWker, I hope this article has added to your repertoire of programming knowledge.

This is a slightly shorter article than the previous ones, but it's an extremely important article.

As always, if you have any questions or want to explore any point further, just reply with your question to this email.

And remember, I'm here to help you improve 1% with each new article!

Awesome HaWker! 👏🏻

We've reached the end of another article. 😮‍💨

And if you made it this far, as usual, please reply to this email with "I managed to understand Data Structures".

This way I'll know that this article series is being useful for you, and that you're following along and taking advantage.

You already know! Only a true HaWker makes it to the end.

Congratulations!

I'm happy to have you here.

See you soon.

Let's go! 🦅

- Jeff Bruchado

Congratulations on making it this far, HaWker! 🎉

Now that you've learned about the main data structures in programming and how to apply them in JavaScript, how about testing your knowledge? Take the quiz below and see how much you absorbed from our article.

It's a great way to reinforce learning and prepare for the next steps in your programming journey.

Click to access the quiz.

Good luck and have fun!

Comments (0)

This article has no comments yet 😢. Be the first! 🚀🦅

Add comments