10 JavaScript concepts every Node developer must master
USMAN AWAN

USMAN AWAN @usman_awan

About: I am a skilled Full Stack web developer with Front-end skills HTML, CSS, JavaScript, ReactJs, NextJs, Bootstrap, Tailwind CSS and Back-end skills NodeJs, ExpressJs, mongoDb, Docker, Vercel and JestJs.

Location:
Abbottabad, Pakistan
Joined:
Sep 23, 2024

10 JavaScript concepts every Node developer must master

Publish Date: Oct 5 '24
535 51

Node.js has quickly become a standard for building web apps and systems software, thanks to its ability to leverage JavaScript on the back end. Popular frameworks like Express and tools like Webpack contribute to its widespread use. Although competitors like Deno and Bun exist, Node remains the leading server-side JavaScript platform.

JavaScript's multiparadigm nature allows for various programming styles, but it also poses risks like scope and object mutation. The lack of tail-call optimization makes large recursive iterations dangerous, and Node’s single-threaded architecture requires asynchronous code for efficiency. Despite its challenges, following key concepts and best practices in JavaScript can help Node.js developers write scalable and efficient code.

1. JavaScript closures

A closure in JavaScript is an inner function that has access to its outer function’s scope, even after the outer function has returned control. A closure makes the variables of the inner function private. Functional programming has exploded in popularity, making closures an essential part of the Node developer’s kit. Here’s a simple example of a closure in JavaScript:

Image description

  • The variable count is assigned an outer function. The outer function runs only once, which sets the counter to zero and returns an inner function. The _counter variable can be accessed only by the inner function, which makes it behave like a private variable.
  • The example here is a higher-order function (or metafunction, a function that takes or returns another function). Closures are found in many other applications. A closure happens anytime you define a function inside another function and the inner function gets both its own scope and access to the parent scope—that is, the inner function can “see” the outer variables, but not vice versa.
  • This also comes in handy with functional methods like map(innerFunction), where innerFunction can make use of variables defined in the outer scope.

2. JavaScript prototypes

Every JavaScript function has a prototype property that is used to attach properties and methods. This property is not enumerable. It allows the developer to attach methods or member functions to its objects. JavaScript supports inheritance only through the prototype property. In case of an inherited object, the prototype property points to the object’s parent. A common approach to attach methods to a function is to use prototypes as shown here:

Image description

Although modern JavaScript has pretty sophisticated class support, it still uses the prototype system under the hood. This is the source of much of the language’s flexibility.

3. Defining private properties using hash names

In the olden days, the convention of prefixing variables with an underscore was used to indicate that a variable was supposed to be private. However, this was just a suggestion and not a platform-enforced restriction. Modern JavaScript offers hashtag private members and methods for classes:

Image description

Private hash names is a newer and very welcome feature in JavaScript! Recent Node versions and browsers support it, and Chrome devtools lets you directly access private variables as a convenience.

4. Defining private properties using closures

Another approach that you will sometimes see for getting around the lack of private properties in JavaScript’s prototype system is using a closure. Modern JavaScript lets you define private properties by using the hashtag prefix, as shown in the above example. However, this does not work for the JavaScript prototype system. Also, this is a trick you will often find in code and its important to understand what it is doing.

Defining private properties using closures lets you simulate a private variable. The member functions that need access to private properties should be defined on the object itself. Here’s the syntax for making private properties using closures:

Image description

5. JavaScript modules

Once upon a time, JavaScript had no module system, and developers devised a clever trick (called the module pattern) to rig up something that would work. As JavaScript evolved, it spawned not one but two module systems: the CommonJS include syntax and the ES6 require syntax.

Node has traditionally used CommonJS, while browsers use ES6. However, recent versions of Node (in the last few years) have also supported ES6. The trend now is to use ES6 modules, and someday we’ll have just one module syntax to use across JavaScript. ES6 looks like so (where we export a default module and then import it):

Image description

You’ll still see CommonJS, and you’ll sometimes need to use it to import a module. Here’s how it looks to export and then import a default module using CommonJS:

Image description

6. Error handling

No matter what language or environment you are in, error handling is essential and unavoidable. Node is no exception. There are three basic ways you’ll deal with errors: try/catch blocks, throwing new errors, and on() handlers.

Blocks with try/catch are the tried-and-true means for capturing errors when things go wrong:

Image description

In this case, we log the error to the console with console.error. You could choose to throw the error, passing it up to the next handler. Note that this breaks code flow execution; that is, the current execution stops and the next error handler up the stack takes over:

Image description

Modern JavaScript offers quite a few useful properties on its Error objects, including Error.stack for getting a look at the stack trace. In the above example, we are setting the Error.message property and Error.cause with the constructor arguments.

Another place you’ll find errors is in asynchronous code blocks where you handle normal outcomes with .then(). In this case, you can use an on(‘error’) handler or onerror event, depending on how the promise returns the errors. Sometimes, the API will give you back an error object as a second return value with the normal value. (If you use await on the async call, you can wrap it in a try/catch to handle any errors.) Here’s a simple example of handling an asynchronous error:

Image description

No matter what, don’t ever swallow errors! I won’t show that here because someone might copy and paste it. Basically, if you catch an error and then do nothing, your program will silently continue operating without any obvious indication that something went wrong. The logic will be broken and you’ll be left to ponder until you find your catch block with nothing in it. (Note, providing a finally{} block without a catch block will cause your errors to be swallowed.)

7. JavaScript currying

Currying is a method of making functions more flexible. With a curried function, you can pass all of the arguments that the function is expecting and get the result, or you can pass only a subset of arguments and receive a function back that waits for the remainder of the arguments. Here’s a simple example of a curry:

Image description

The original curried function can be called directly by passing each of the parameters in a separate set of parentheses, one after the other:

Image description

This is an interesting technique that allows you to create function factories, where the outer functions let you partially configure the inner one. For example, you could also use the above curried function like so:

Image description

In real-world usage, this idea can be a help when you need to create many functions that vary according to certain parameters.

8. JavaScript apply, call, and bind methods

Although it’s not every day that we use them, it’s good to understand what the call, apply, and bind methods are. Here, we are dealing with some serious language flexibility. At heart, these methods allow you to specify what the this keyword resolves to.

In all three functions, the first argument is always the this value, or context, that you want to give to the function.

Of the three, call is the easiest. It’s the same as invoking a function while specifying its context. Here’s an example:

Image description

Note that apply is nearly the same as call. The only difference is that you pass arguments as an array and not separately. Arrays are easier to manipulate in JavaScript, opening a larger number of possibilities for working with functions. Here’s an example using apply and call:

Image description

The bind method allows you to pass arguments to a function without invoking it. A new function is returned with arguments bounded preceding any further arguments. Here’s an example:

Image description

9. JavaScript memoization

Memoization is an optimization technique that speeds up function execution by storing results of expensive operations and returning the cached results when the same set of inputs occur again. JavaScript objects behave like associative arrays, making it easy to implement memoization in JavaScript. Here’s how to convert a recursive factorial function into a memoized factorial function:

Image description

10. JavaScript IIFE

An immediately invoked function expression (IIFE) is a function that is executed as soon as it is created. It has no connection with any events or asynchronous execution. You can define an IIFE as shown here:

Image description

The first pair of parentheses function(){...} converts the code inside the parentheses into an expression.The second pair of parentheses calls the function resulting from the expression. An IIFE can also be described as a self-invoking anonymous function. Its most common usage is to limit the scope of a variable made via var or to encapsulate context to avoid name collisions.

There are also situations where you need to call a function using await, but you’re not inside an async function block. This happens sometimes in files that you want to be executable directly and also imported as a module. You can wrap such a function call in an IIFE block like so:

Image description

11. Useful argument features

Although JavaScript doesn’t support method overloading (because it can handle arbitrary argument counts on functions), it does have several powerful facilities for dealing with arguments. For one, you can define a function or method with default values:

Image description

You can also accept and handle all the arguments at once, so that you can handle any number of arguments passed in. This uses the rest operator to collect all the arguments into an array:

Image description

If you really need to deal with differing argument configurations, you can always check them:

Image description

Also, remember that JavaScript includes a built-in arguments array. Every function or method automatically gives you the arguments variable, holding all the arguments passed to the call.

Conclusion

As you become familiar with Node, you’ll notice there are many ways to solve almost every problem. The right approach isn’t always obvious. Sometimes, there are several valid approaches to a given situation. Knowing about the many options available helps.

The 10 JavaScript concepts discussed here are basics every Node developer will benefit from knowing. But they’re the tip of the iceberg. JavaScript is a powerful and complex language. The more you use it, the more you will understand how vast JavaScript really is, and how much you can do with it.

Comments 51 total

  • Adnan Babakan (he/him)
    Adnan Babakan (he/him)Oct 6, 2024

    Good points to remember in JS. I really enjoyed reading it.

    • USMAN AWAN
      USMAN AWANOct 6, 2024

      Thank you very much 😊

      • Lisa Linda
        Lisa LindaOct 12, 2024

        I want to use this medium to testify of how I got back my ex husband after divorce, i and my husband and I have been married for 8 years with 2 kids, we have been a happy family. Last year his behavior towards me and the kids changed, i suspected he was meeting another woman outside out marriage, any time i confronted him, he threatened to divorce me, i did all i could to make things right but all to no avail until i saw a post on a "love and relationship forum" about a spell caster who helps people to cast spell on marriage and relationship issues, when i contacted this spell caster via email, he helped me cast a reunion spell and my husband changed and came apologizing to me and the kids. Contact this great spell caster for your relationship or marriage issues via his
        email (osofo.48hoursolutioncenter@gmail.com ) or Whatsapp him on +2349065749952 Good luck

  • Adesh Tamrakar
    Adesh TamrakarOct 7, 2024

    This is going to be my everytime go-to revision list. Thanks.

  • Serhiy
    SerhiyOct 7, 2024

    but why do you use var?

    • WORMSS
      WORMSSOct 8, 2024

      I am guessing someone who likes the nasty habit of hoisting. I would put money on them not being fans of Typescript.
      I don't mind function hoisting, but I don't know of anyone who likes variable hoisting.
      Though, I find people who use let for absolutely everything to be strange also.. I use const for everything unless I absolutely HAVE to use a reassigning variable, which is maybe only 1% of the time at most.

      • Jonathan DS
        Jonathan DSOct 9, 2024

        Is it hoisting or just global scope?

        • WORMSS
          WORMSSOct 9, 2024

          Hoisting. Because if you are inside another scope (closure for example) it's no longer globally available.

    • minoblue
      minoblueOct 14, 2024

      One usecase is when we want to access variable outside the block within the function.

      function example() {
        if (true) {
          var x = 10;  // 'var' is function-scoped, not block-scoped
          let y = 20; // 'let' is block-scoped
        }
        console.log(x); // Accessing 'x' outside the 'if' block
        console.log(y); // ReferenceError: y is not defined
      }
      
      
      
      Enter fullscreen mode Exit fullscreen mode
  • almogzur
    almogzurOct 7, 2024

    Vary nice

  • Friday candour
    Friday candourOct 7, 2024

    It's for beginners

    • Marc
      MarcOct 7, 2024

      advanced beginners ;-)

      • Lisa Linda
        Lisa LindaOct 12, 2024

        I want to use this medium to testify of how I got back my ex husband after divorce, i and my husband and I have been married for 8 years with 2 kids, we have been a happy family. Last year his behavior towards me and the kids changed, i suspected he was meeting another woman outside out marriage, any time i confronted him, he threatened to divorce me, i did all i could to make things right but all to no avail until i saw a post on a "love and relationship forum" about a spell caster who helps people to cast spell on marriage and relationship issues, when i contacted this spell caster via email, he helped me cast a reunion spell and my husband changed and came apologizing to me and the kids. Contact this great spell caster for your relationship or marriage issues via his
        email (osofo.48hoursolutioncenter@gmail.com ) or Whatsapp him on +2349065749952 Good luck

    • WORMSS
      WORMSSOct 8, 2024

      Technically it's for 'everyone'.
      Beginners will be learning about it, and for advanced people, it's a checkbox exercise to say "yep, I know these concepts".. It's not like, once you are past the beginners stage, you can forget these concepts..
      It's always good to remember that async/await is sugar for Promises/then, and class declarations is sugar for prototype. It's good to remember that these are what is happening under the hood. Even if you don't touch these old ways anymore.

      • Mohammad Zeeshan
        Mohammad ZeeshanDec 10, 2024

        Absolutely agree! Understanding these concepts are the backbone of JavaScript and serve as a reminder of what’s happening under the hood. Even if advanced devs rarely use the 'old ways,' knowing them enhances debugging, performance optimization, and deepens overall JavaScript expertise. For beginners, it's a foundation to build on. Revisiting these basics is always worthwhile—it ensures we don’t just write code but truly understand how it works at its core.

  • Robert Snedeker
    Robert SnedekerOct 7, 2024

    Great article! I recently discovered the # private property/method thing in JS. But now that I use TS it's not something I use, still cool to see that JS is getting more OOP features though.

  • shield8994
    shield8994Oct 7, 2024

    Your article is well written. I am the operator of servbay. Can you promote our products? Our official website is servbay.com. If you are willing, we can give you some channel discounts.

    • USMAN AWAN
      USMAN AWANOct 7, 2024

      I would be an honour 😊😋

      • shield8994
        shield8994Oct 8, 2024

        How can I contact you? Email?

        • Lisa Linda
          Lisa LindaOct 12, 2024

          I want to use this medium to testify of how I got back my ex husband after divorce, i and my husband and I have been married for 8 years with 2 kids, we have been a happy family. Last year his behavior towards me and the kids changed, i suspected he was meeting another woman outside out marriage, any time i confronted him, he threatened to divorce me, i did all i could to make things right but all to no avail until i saw a post on a "love and relationship forum" about a spell caster who helps people to cast spell on marriage and relationship issues, when i contacted this spell caster via email, he helped me cast a reunion spell and my husband changed and came apologizing to me and the kids. Contact this great spell caster for your relationship or marriage issues via his
          email (osofo.48hoursolutioncenter@gmail.com ) or Whatsapp him on +2349065749952 Good luck

      • Lisa Linda
        Lisa LindaOct 12, 2024

        I want to use this medium to testify of how I got back my ex husband after divorce, i and my husband and I have been married for 8 years with 2 kids, we have been a happy family. Last year his behavior towards me and the kids changed, i suspected he was meeting another woman outside out marriage, any time i confronted him, he threatened to divorce me, i did all i could to make things right but all to no avail until i saw a post on a "love and relationship forum" about a spell caster who helps people to cast spell on marriage and relationship issues, when i contacted this spell caster via email, he helped me cast a reunion spell and my husband changed and came apologizing to me and the kids. Contact this great spell caster for your relationship or marriage issues via his
        email (osofo.48hoursolutioncenter@gmail.com ) or Whatsapp him on +2349065749952 Good luck

    • Abid
      AbidOct 8, 2024

      Hi, is it not for windows users? I can not find any link for windows on your website.

    • Rudra Bharti
      Rudra BhartiOct 8, 2024

      Wow Rally Nice Government Jobs Portal

  • Brad
    BradOct 7, 2024

    I gotta point it out.

    It seems like the code examples have inconsistent indentation? 🤔

    • Lisa Linda
      Lisa LindaOct 12, 2024

      I want to use this medium to testify of how I got back my ex husband after divorce, i and my husband and I have been married for 8 years with 2 kids, we have been a happy family. Last year his behavior towards me and the kids changed, i suspected he was meeting another woman outside out marriage, any time i confronted him, he threatened to divorce me, i did all i could to make things right but all to no avail until i saw a post on a "love and relationship forum" about a spell caster who helps people to cast spell on marriage and relationship issues, when i contacted this spell caster via email, he helped me cast a reunion spell and my husband changed and came apologizing to me and the kids. Contact this great spell caster for your relationship or marriage issues via his
      email (osofo.48hoursolutioncenter@gmail.com ) or Whatsapp him on +2349065749952 Good luck

  • Yurii Timofeev
    Yurii TimofeevOct 8, 2024

    Good advice... for 2014

  • Yair Nevet
    Yair NevetOct 8, 2024

    Great post! However, it's a shame that the code examples are presented as images instead of regular text, as it makes it harder for readers to copy and test the code themselves.

  • Vivek Garg
    Vivek GargOct 8, 2024

    Love these insights keep sharing such valuable thoughts.

  • RamR
    RamROct 8, 2024

    nice post

  • Shahadat Hossain
    Shahadat HossainOct 8, 2024

    Javascript and Node becoming more popular among developers. it is not bind only for web applications; we are building more complex applications like IOT, machine learning, chatbots, ect.
    it is a really game changer for developer.

    • Lisa Linda
      Lisa LindaOct 12, 2024

      I want to use this medium to testify of how I got back my ex husband after divorce, i and my husband and I have been married for 8 years with 2 kids, we have been a happy family. Last year his behavior towards me and the kids changed, i suspected he was meeting another woman outside out marriage, any time i confronted him, he threatened to divorce me, i did all i could to make things right but all to no avail until i saw a post on a "love and relationship forum" about a spell caster who helps people to cast spell on marriage and relationship issues, when i contacted this spell caster via email, he helped me cast a reunion spell and my husband changed and came apologizing to me and the kids. Contact this great spell caster for your relationship or marriage issues via his
      email (osofo.48hoursolutioncenter@gmail.com ) or Whatsapp him on +2349065749952 Good luck

  • ArsalanNury
    ArsalanNuryOct 8, 2024

    Thanks, I haven't seen anyone use IIFE in real programs. I think just Webpack and Babel are use it :-))))

    I find rest operators the most useful section in this article my friend

    • Alexander Borshak
      Alexander BorshakOct 9, 2024

      Did you ever use jQuery? Aside of it, IIFE used often when you have to perform some actions during init stage of ESM module.

    • José Pablo Ramírez Vargas
      José Pablo Ramírez VargasOct 11, 2024

      UMD modules are basically IIFE's. AngularJS had heavy use of IIFE's. Finally, CommonJS can see them used to encapsulate await syntax.

  • Tom Pereira
    Tom PereiraOct 8, 2024

    Shouldn't:

    the CommonJS include syntax and the ES6 require syntax.

    ...read:

    the CommonJS require syntax and the ES6 include syntax.

    • William Bernfield
      William BernfieldOct 10, 2024

      Hello everyone, id live to get some help, Also how do you all feel about Quantum capabilities ? Early access

  • Axorax
    AxoraxOct 8, 2024

    cool article :D

  • Refaat Atya
    Refaat AtyaOct 9, 2024

    Carlos james, whose contact email is recoveryfundprovider@gmail. com, has established himself as a highly skilled professional specializing in the recovery of stolen USDT, a popular cryptocurrency. With a proven track record in this niche area, he possesses the necessary expertise to assist individuals facing the distressing situation of asset theft. In addition to his email contact, Carlos is also active on his mail box For those who prefer direct communication. His comprehensive understanding of cryptocurrency recovery makes him a valuable resource for anyone seeking to retrieve their lost digital assets.

  • The Eagle 🦅
    The Eagle 🦅Oct 9, 2024

    I've had fun reading this article, thanks bro!

  • Zohaib Malik
    Zohaib MalikOct 9, 2024

    Looking for a thrilling mobile gaming experience? Check out K9 Game, available for download at S9-Game.cc. Enjoy action-packed gameplay, immersive graphics, and smooth performance on any device. Get the K9 Game APK today for an exciting adventure right at your fingertips!

  • Rafa Rios
    Rafa RiosOct 9, 2024

    Nice article to bear in mind. Thanks for the clear examples

  • sachin kotian
    sachin kotianOct 9, 2024

    nice article but try pasting code instead of screenshots

  • retrop5
    retrop5Oct 10, 2024

    The first I learnt / heard of curry functions and I love it, I am gonna keep using it extensively moving forward

  • Softwaredevelopmentinsights
    SoftwaredevelopmentinsightsOct 10, 2024

    Great insights on JS. It is very informative.

  • David Sabalete
    David SabaleteOct 11, 2024

    Thank you!

  • Lisa Linda
    Lisa LindaOct 12, 2024

    I want to use this medium to testify of how I got back my ex husband after divorce, i and my husband and I have been married for 8 years with 2 kids, we have been a happy family. Last year his behavior towards me and the kids changed, i suspected he was meeting another woman outside out marriage, any time i confronted him, he threatened to divorce me, i did all i could to make things right but all to no avail until i saw a post on a "love and relationship forum" about a spell caster who helps people to cast spell on marriage and relationship issues, when i contacted this spell caster via email, he helped me cast a reunion spell and my husband changed and came apologizing to me and the kids. Contact this great spell caster for your relationship or marriage issues via his
    email (osofo.48hoursolutioncenter@gmail.com ) or Whatsapp him on +2349065749952 Good luck

  • Xiong Leia
    Xiong LeiaOct 24, 2024

    Hello, my name is Xiong Leia  , and I was able to get my funds refunded from a very shady asset manager. Last year, I put all of my life savings into cryptocurrency, but I was compromised in the process, which ruined my entire life. I used  George Wizard Recovery Home  in January after a friend recommended them to me, and I received a full refund. Without George Wizard Recovery ,I would still be in the dark. All thanks to this George Wizard Recovery Home  , my horror is finally ended; it's a brand-new year with a brand-new beginning. Please be careful while dealing with investors out there and contact George Wizard Recovery Home  if you find yourself in a situation like this contact them (georgewizardrecoveryhome AT gmail DOT com  )whatsapp: +1 (908) 768-4663

  • killer space
    killer spaceNov 8, 2024

    broo ,really this is way advanced for a beginner in js

  • Florence Grace
    Florence GraceFeb 7, 2025

    Ever since my husband got me divorced for the past 2 years, i v'e not been my self. I was reviewing some post of how i could get back my husband then, i saw a testimony shared by Marina Choas from SWEDEN about a spell caster named Dr. Okosun. I contacted Marina Chaos to confirm about how Dr. Okosun helped her and she clarified everything to me of how he helped her and that gave me the courage to get in touch with Dr. OKosun for help. Dr. Okosun assured me that my days of sorrows will be over within 48hours after he has finished with his work. I followed his instructions he gave to me because i had the believe, faith, hope and trust in him. Verily i say to you today that i and my husband are back together and i can proudly say and testify to the world of what Dr. Okosun did for me. Contact him today via E-mail:(Okosunspelltemple33@gmail.com call him or whatsapp him +2348054338132 if you seek his help.

Add comment