DEV Community

Cover image for JavaScript Essentials: A Beginner's Guide to Data Structures and Algorithms

JavaScript Essentials: A Beginner's Guide to Data Structures and Algorithms

Matt Ryan on December 05, 2023

If you're looking to get your JavaScript skills to the next level, you're in the right place. Let's jump right in! πŸŽ‰ What are Data Struct...
Collapse
 
cedriclapi profile image
CedricLapi

i love the analogies you've used to explain these concepts, as a beginner i find it quite insightful!

Collapse
 
efpage profile image
Eckehard • Edited

Unlike structures in other languages, Javascript cannot limit the elements in an object in any way. This is especially bad because even const arrays can be extended:

const car = {
  make: "Toyota",
  model: "Corolla",
  year: 2021
};
car.color = "red"

Object.freeze(car);
car.fruit = "Bananna"

console.log(car) => {make: 'Toyota', model: 'Corolla', year: 2021, color: 'red'}
Enter fullscreen mode Exit fullscreen mode

You can prevent extension by freezing the object, but you will not get an error message.

The only way I know to ensure that certain properties are defined ist this:

function setTemplate(target, template) {
    Object.keys(template).forEach(key => target[key] = target[key] ?? template[key])
}

const car = {
    make: "Toyota",
    model: "Corolla",
    year: 2021
};

const template = {
    make: "-",
    model: "-",
    year: "-",
    size: "-",
    length: "-"
}

setTemplate(car, template)

console.log(car) => {make: 'Toyota', model: 'Corolla', year: 2021, size: '-', length: '-'}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
manchicken profile image
Mike Stemle

I wish there were more detail. When would you use a structure? Where would you avoid its use? How are the structures more helpful than similar alternatives? Same for the algorithms.

Good stuff, it’s just missing a lot of detail.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Probably worth pointing out that binary search requires a sorted array.

Collapse
 
artxe2 profile image
Yeom suyun

There seems to be a problem with the algorithm implementation in the code.
Even excluding minor optimizations, merge sort includes empty left and right in the return, while quicksort employs unnecessary left and right.

Collapse
 
just_another_react_dev profile image
Just another React Dev

Very informative and concise no brainer post

Collapse
 
codebrewlabsusa profile image
codebrewlabsusa

very good information

Collapse
 
devgancode profile image
Ganesh Patil

Great Explanation πŸŽ‰
I just want to ask one question Doing DSA in JS is good decision?

Collapse
 
xi profile image
xi

nice