When to use OOP in Javascript

I have learned Javascript for a while but what really confuse me right now is that i rarely need to use OOP in Javascript. I feel like i’m missing something, so i would like to ask when you would use OOP in Javascript and when would you use functional programming. Thank all !!!

This is a great question - I am a junior developer (especially with JS) and I was hoping someone would shed some light on this as well.

Here are my 2 cents for what they are worth.

I think it depends on the requirements and the design of the project you are building.

If I needed to build a server side banking application - I might decide at the design stage there is going to be an ACCOUNT entity.

I might decide a user should be able to do things on that account like CREDIT or DEBIT

I might decide these CREDIT and DEBIT functions should only exist on instances of an ACCOUNT object - meaning the functionality is always kept in one single place - meaning less chance of code duplication and controlling access to these methods

I might decide some users have different TYPES OF ACCOUNTS, some may have a StandardAccount others a SuperAccount

At this point - I would probably consider using OOP techniques here is some very badly written JS (i rarely use it at the moment as an example)

class Account

class SuperAccount extends Account

So that’s not a very good explanation but whenever I think about encapsulating properties and methods (functions) inside of a single reusable object - where inheritance may be involved - I think about OOP - I think quite a bit of server side JS is written that way .

When I’m thinking more about logic that I want to express as a series of reusable functions that can be composed together to do different tasks - I might start to think about functional programming.

If you were building something that carried out a cooking recipe - you might want the program to be a series of functions like

function boilIngredient();
function grillIngredient();
function setCookingTimer();
function placeInFridge();

At that point i would think more about functional programming.

Probably not the right answer, but just my 2 cents.

I’m not sure if there is a hard rule of when you should or shouldn’t. Almost all problems can be handled either way, but there are situations where taking a specific approach turns out better and easier to understand.

For example, if your building a game taking an overall OOP approach will help you organize different parts of the game into a paradigm that helps you represent different parts of the game. IE Everything on the “map” has coordinates, and thus can inherit some generic class to help re-use logic for stuff that is placed on the map. Most game engines take this approach as its easier to track and follow mentally.

On the flip side, you could use OOP for building complex state transitions, but taking a more functional approach would allow you to leverage all the advantages of a functional programming approach could give such as no side-effects.

Its worth noting JavaScript’s OOP approach is prototype based, and most common “helper” api’s are available from classes provided out of the box. However, JavaScript as a whole is multi-paradigm so you can go OOP or Functional when you want how you want and it should be OK with it.

I got it thank everyone