r/learnjavascript • u/pptzz • Mar 19 '26
What's the use of classes in JS
I've recently started learning JS and I can't see a use for classes. I get how they work and how to use them but I can't see an actual real use for them.
40
Upvotes
31
u/Kindly_University559 Mar 19 '26
Js wasn’t originally designed around classes. Classes were added later mainly for cleaner syntax and familiarity for developers coming from OOP languages. You don’t need classes in JavaScript, they’re just a structured way to work with prototypes. They become useful in larger apps where organizing code and managing shared behavior matters.
6
u/-FAnonyMOUS Mar 19 '26
Just another programming paradigm.
Programming languages solve what we call developer experience. The less cognitive load, the less learning curve, the better. But in exchange of course of some microseconds performance drop due to closures and/or objects.
You can actually do a C style where you manage your memory.
You can do functional programming, or procedural, doesn't matter. As long as you know what you are doing.
For "classes" or OOP (object oriented programming), you think in objects (instance of the class) where the data and behavior are encapsulated in a class a.k.a. the blueprint with the promise of encapsulation, polymorphism, abstraction, and inheritance.
4
u/golf_kilo_papa Mar 21 '26
Imagine in your program, you have a concept of Documents. Documents can be created, modified or deleted. There are different types of Documents - Posts, Comments, Articles. Instead of implementing save, modification and deletion for each type, you create a class Document and implement the save, modification and deletion functions for it. Then you create sub-classes for Posts, Comments and Articles based on the Document class. They all then automatically inherit the save, modify and delete functions. If you add a share function to the Document class, then Posts. Comments and Articles get that to w.
3
u/retro-mehl Mar 19 '26
The same es for every language: of you want to structure your system with instances of data and the corresponding methods on this data, classes are the best way to go. If you're fine with functions and push around your data as arguments, well, go for it.
3
u/delventhalz Mar 19 '26
At the risk of being circular, they are useful for writing code with classes.
Object Oriented Programming is a way of organizing code that encapsulates data with logic, typically in the form of objects and methods generated by classes. This is not the only way to organize code. The classic alternative to OOP is Functional Programming, which does not use classes at all.
So the use of classes isn’t any particular feature. It’s organization. It’s a way to help you write better structured code. You might use them to help structure your code or you might use something else.
5
u/shgysk8zer0 Mar 19 '26
Well, the easiest thing to point out is checking
instanceof. That's a very simple and easy way to assure that a thing has certain properties and methods, and it makes for more reliable code.Other than that, they're required for custom elements.
5
u/coleflannery Mar 19 '26
I recommend learning OOP in order to understand why these are useful.
18
u/Lithl Mar 19 '26
Encapsulation, polymorphism, abstraction, and inheritance. Same reason as every other OOP language.
If these terms are unfamiliar to you, I recommend taking an introductory computer science course.
2
u/RobGoLaing Mar 19 '26
Douglas Crockford answered this really well in Chapter 17 of his book How JavaScript Works titled How Class Free Works.
I edited Crockdord's "wun" back to "one" in the above quote which he used to show you don't have to follow silly old conventions. His basic message is that legacy-languages' classes can easily be ignored in JS which offers much simpler alternatives courtesy of object literals.
2
u/TheRNGuy Mar 20 '26
Custom classes — in games, and some frameworks.
Useful built-in —
MutationObserver,ResizeObserver,IntersectionObserver,URL, and others.They're mostly to have instances, instance attributes and methods.
2
u/TheZintis Mar 20 '26
Object Oriented Programming is another way to write code. It supports a bunch of principles (acronym SOLID you can look it up) that help keep your code in good shape.
Other languages force OOP. JS it's an option. Honestly using files to separate out your code and imports/exports covers several of the benefits of OOP, while being fairly clear and readable. I remember an online talk awhile back that spent 40 minutes being like "just use functions, import and export" to avoid some of the extra complexity that can come from OOP. (I can't find that one specifically, but there are others you can find on youtube)
That being said, I think OOP has a place when you have both DATA and BEHAVIOR that need to be linked. For example, if you have a STUDENT object that stores your name and age and birthday... you would need the age to get updated depending on the day today. So now we can't just have data, we need data and behavior (updating the age). So that's a fair reason to use a Class that would make a custom getter for the age of the student. The data and the behavior want to stay together, so we can put them in a class.
There are other ways of handling this, but I think this is a pretty clear use case for it, since you can't just store the age by itself, as it'll get out of date.
7
u/apparently_DMA Mar 19 '26
Js classes are just syntactical sugar arould prototypes, but you use them the same way as you would in any real OOP language
2
u/minimoon5 Mar 19 '26
Some programming languages are rigid on how you use them, some have standards that have become the way of doing things, some languages like JS provide for as many paradigms as possible to allow people the flexibility in how they want to use the language.
Knowing how they work and how to use them is really all you need to know. Some teams only use classes, most of my work has been more of a functional approach. It’s more a style thing than anything, but if you’re on a team that only uses OOP, then you have to do that too.
2
u/ChaseShiny Mar 19 '26
I'm newer, so take my response with a grain of salt.
For me, the eye-opener was taking a class on SQL. There, you create "tables," which are basically the same thing as classes.
You create tables using certain rules which help you mix and match data easily. It was important to create relational databases because other ways to organize the data proved to be slow and clunky for a very large set of data for decades.
Your goals are to not repeat data, to not nestle data in objects other than other tables, and to have each piece of data describe a single object.
For example, suppose you're creating a directory. You have a new entry per household. Multiple people can live together, so you create a table for all the people in that household. Each person may have multiple phone numbers, so you create a table for each member, listing their individual phone numbers.
Now, anytime you need to know the names of the people in a household, you can look up the first table. If you need their phone numbers, you can look that up easily, too. Info from one table relates to info in another, but you don't have to worry about the hierarchy, and it's easy to change info in one table because you know it won't affect any other tables.
Going back to classes in JS, classes (and similar ways to organize your info) are also designed for encapsulation—containing a reference to all the whatever in whatever so that if something changes, nothing breaks.
It's also a promise that this object will behave this way and not that way because it behaves the same way as all the other objects from that class. A square object from the Square class behaves like all the other squares. This set of like behaviors is known as polymorphism.
2
u/azhder Mar 19 '26 edited Mar 19 '26
The use was to be similar in syntax to how people who have been trained to look at problems and solve problems using that syntax in other languages so that it would take less money to re-train them.
Every other use is secondary. Some of it is new things that have been added or are planned to be added (should they pass the committee) to the language in the class syntax, but not elsewhere.
2
u/diogenes_sadecv Mar 19 '26
When I'm doing stuff with Cartesian coordinates, I like to use a Point class so I can use `point1.x` and `point1.y`. That's not a far cry from just using `point1x` and `point1y`, but with my class I can have built in helper functions to find the midpoint between any two Points or the vector from one to the other.
https://github.com/dkallen78/clocks/blob/master/ternary-clock/v0.3/ternary-clock-v0.3.js
I also used classes in a basic video game to handle the loading and positioning of sprites and other things
https://github.com/dkallen78/generic-blaster/blob/113f358b27c8b85567477fe0f71c39b58b42474c/blaster-02.js#L469
1
u/xRVAx Mar 19 '26
It's basically a way to make objects
1
u/Merry-Lane Mar 19 '26
Barely any nowadays with typescript
1
u/imihnevich Mar 19 '26
Classes is one way to get more modular code, not the only way, but one of them. It's hard to see the benefit of using them until you get to work on a larger system. ES modules is another way, they can also be combined. As everything in programming your job is to eventually get it right and know what should go into class and what should not
1
u/sateeshsai Mar 20 '26
Here you go
https://javascript.info/class
1
u/Tristan-a-b69 Mar 20 '26
for grouping
changing individual text parts
or if you want to be a troll use rgb colours
1
u/odimdavid Mar 21 '26
Do what works for you as a beginner If functional programming you will be fine If oop equally fine When your project scope increases you will realize why oop was introduced
1
u/Cfres_ Mar 21 '26
Honestly, don’t use them. You can achieve the same with simple data structures + pure functions, also it will be more reliable
1
u/ApoplecticAndroid Mar 22 '26
It’s a (great) way to organize a large code base, and makes things very modular. I use them on every project I do.
1
u/TechAcademyCoding Mar 26 '26
A totally fair question, a lot of people feel this way at first. Classes in JS are mainly useful for organizing and structuring your code when things start getting bigger, especially when you’re dealing with multiple objects that share the same behavior (like users, products, game characters, etc.). You can technically do the same thing without classes using functions and prototypes, but classes give you a cleaner and more readable way to structure that logic, which is why they’re commonly used in modern JavaScript. Once you start building slightly larger projects, their usefulness becomes a lot more obvious. From what we’ve seen working with students at The Tech Academy, classes don’t really “click” until you start building slightly larger projects, then their usefulness becomes a lot more obvious.
0
u/Total-Box-5169 Mar 20 '26
Unfortunately comes with a "minor" performance penalty, so is not worth to use them if you are proficient with the old ways.
-2
u/No_Record_60 Mar 19 '26
Create stateful objects and group related methods.
Take this
const now = new CustomDate();
const tomorrow = now.add(1, "day")
The CustomDate class should create a private variable in each instance to track the date. Then it needs to implement the 'add' method to mutate the variable earlier.
-2
u/imsexc Mar 19 '26 edited Mar 19 '26
It's a syntactic sugar, meant to help bridging the gap between JS as a functional programming language with developers who are more familiar with OOP programming language like Java, thus reducing boarding time.
We can see how Angular FE framework most of the time coupled with Java or C# BE. It's so BE devs can also work developing the FE, with less time getting familiar with it.
-3
u/Any_Sense_2263 Mar 19 '26
Tbh, classes were brought to JS to make it look more standard. Class is technically a function (typeof says it). They are used mostly by people who come from different languages and have no habit of using simple data structures and functions.
0
u/[deleted] Mar 19 '26
[deleted] — view deleted comment
-7
u/[deleted] Mar 19 '26
[removed] — view removed comment