Write Node like it's 2022

Rate this content

I was seeing some code that was written a few years ago, that was considered the "super clean" and "intuitive", and I realised that the JavaScript that we write today is very different from the one that we used to write just five years ago.

If you've been around for some time, you know the feeling of writing "this = that", and taking days debugging why that variable is set to null. The new node syntax, with esm, new bundlers, faster refreshes. The code was never so easy to write and understand.

You can check the slides for Pedro's talk here.

7 min
18 Feb, 2022

AI Generated Video Summary

Let's write Node like it's 2022. We can now iterate over objects more easily with the for-in loop. Template literals make string concatenation and multi-line strings much simpler. The structured clone feature allows us to clone complex objects without the need for hacks like stringifying and parsing. Default parameters in JavaScript are now much more convenient. Async await eliminates the need for callbacks. Arrow functions inherit the scope, eliminating the need for 'this equals that'. Const prevents variable reassignment. Let helps catch errors earlier. Write code like it's 2022!

1. Introduction to Writing Node in 2022

Short description:

Let's write Node like it's 2022. My name is Pedro, a software engineer at blockchain.com. I've been evolving with Node and I'm proud of it.

Hi. Let's write Node like it's 2022. Well, first of all, who am I? My name is Pedro, and I'm a software engineer at blockchain.com. And, well, I was looking at the code I wrote a few years ago, like 10 years ago, and I saw that my life was very, very challenging and fun. But nowadays, it's just fun. I wrote a bunch of this equals that, and I used a lot of low dash underscores, and everything else that you can think of. Just make some stuff that work as expected today. Node evolved, and I've been evolving with it, and I'm very proud of it.

2. Writing Node in 2022

Short description:

Let me tell you how to write code node like it's 2022. We can now iterate over objects more easily with the for-in loop. Template literals make string concatenation and multi-line strings much simpler. The structured clone feature allows us to clone complex objects without the need for hacks like stringifying and parsing. Default parameters in JavaScript are now much more convenient.

So, let me tell you how to write code node like it's 2022. Look at this example, for example. So, we had to use multiple methods to iterate over an object. So, in this get total score, we had to use object keys and a for each to access the property inside of the object, and then assign the value to the totals. But now, we can do a for in, and we can access the key of the object And we can do it very easily, and with only one method. So, it's way easier to read, it's easy to return about. I know what I'm reading as soon as I put my eyes on it, way better.

Well, we had to concat strings to get phrases with variables, and LongAVI started with multi lines. These were very, very hard to create. In this example, I just created one that puts together three strings in this case. I had to use strings to spaces because I hated to add space in the end of this one, and here, I really need it because I can't add anything to this string directly. But now, I can use template literals and I can just make it work. I can do multi lines easily and everything I see is exactly what appears in a screen. So, much easier.

We had to use the hack to stringify and parse objects to clone them. And this wasn't a case for simple objects. You could do other stuff. And in load dash we had clone deep that made exactly the same thing as this code does. That is, basically get an object with complex and nested data and put it in another variable. As JavaScript would use a pointer to this one. If I only get like B equals A, I would get the value assigned as just a pointer to A and not actually a value of A. So I had to transform it in a string and then parse it to actually clone the value of the object. Well, that's a very complicated explanation to just copy an object. But now we can use structured clone. That is exactly what it says. It's a structured clone with all the properties, nested ones, and it just works as expected. I mean, it's very nice.

What about the default parameters? Oh, this one. Everyone that writes JavaScript in 2022 knows that the default parameters are very, very nice. Back in the day, you had to in the top of every function, you would have the variable equals variable or something, just like here.

3. Writing Code Like It's 2022

Short description:

A equals A or 1. You can pass default parameters right in the function. Async await eliminates the need for callbacks. Arrow functions inherit the scope, eliminating the need for 'this equals that'. Const prevents variable reassignment. Let helps catch errors earlier. Write code like it's 2022!

A equals A or 1. And in some cases, in cases of objects, you would have a bunch of them listed at the top of the function, making it very hard to read. But today is not the case. Because today, you can do it like A equals 1 and B equals 2 in the top.

So on the function, you can literally pass the default parameters right there without any other fuss. And Nogami started with async await. Now you don't need callbacks to handle HTTP requests. You can use async await. I'm an async await person but you can use like then that is very helpful to write like async code. You have arrow functions that don't have this scope and they inherit the scope. So you don't have to write this equals that in a bunch of cases. You have const that doesn't let you reassign the variable. You have let that makes it easier to see the errors before you see them because with the var it would hoist to the top and you would get a bunch of cannot read a often defined for example. And there are other things that I don't remember right now but are very very important.

So well do a favor to everyone and write like a 2022. It's not that hard. Well that's my GitHub and my Twitter. And that's all folks. Let me know if you have any questions.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Do you know what’s really going on in your node_modules folder? Software supply chain attacks have exploded over the past 12 months and they’re only accelerating in 2022 and beyond. We’ll dive into examples of recent supply chain attacks and what concrete steps you can take to protect your team from this emerging threat.
You can check the slides for Feross' talk here.
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
In the early years of Node.js, diagnostics and debugging were considerable pain points. Modern versions of Node have improved considerably in these areas. Features like async stack traces, heap snapshots, and CPU profiling no longer require third party modules or modifications to application source code. This talk explores the various diagnostic features that have recently been built into Node.
You can check the slides for Colin's talk here. 
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
Native ESM support for Node.js was a chance for the Node.js project to release official support for enhancing the module loading experience, to enable use cases such as on the fly transpilation, module stubbing, support for loading modules from HTTP, and monitoring.
While CommonJS has support for all this, it was never officially supported and was done by hacking into the Node.js runtime code. ESM has fixed all this. We will look at the architecture of ESM loading in Node.js, and discuss the loader API that supports enhancing it. We will also look into advanced features such as loader chaining and off thread execution.
JSNation Live 2021JSNation Live 2021
19 min
Multithreaded Logging with Pino
Almost every developer thinks that adding one more log line would not decrease the performance of their server... until logging becomes the biggest bottleneck for their systems! We created one of the fastest JSON loggers for Node.js: pino. One of our key decisions was to remove all "transport" to another process (or infrastructure): it reduced both CPU and memory consumption, removing any bottleneck from logging. However, this created friction and lowered the developer experience of using Pino and in-process transports is the most asked feature our user.In the upcoming version 7, we will solve this problem and increase throughput at the same time: we are introducing pino.transport() to start a worker thread that you can use to transfer your logs safely to other destinations, without sacrificing neither performance nor the developer experience.

Workshops on related topic

Node Congress 2023Node Congress 2023
109 min
Node.js Masterclass
Have you ever struggled with designing and structuring your Node.js applications? Building applications that are well organised, testable and extendable is not always easy. It can often turn out to be a lot more complicated than you expect it to be. In this live event Matteo will show you how he builds Node.js applications from scratch. You’ll learn how he approaches application design, and the philosophies that he applies to create modular, maintainable and effective applications.

Level: intermediate
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
JSNation 2023JSNation 2023
104 min
Build and Deploy a Backend With Fastify & Platformatic
Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js (https://oss.platformatic.dev/).- Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow (https://platformatic.dev/). 
In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.
JSNation Live 2021JSNation Live 2021
156 min
Building a Hyper Fast Web Server with Deno
Deno 1.9 introduced a new web server API that takes advantage of Hyper, a fast and correct HTTP implementation for Rust. Using this API instead of the std/http implementation increases performance and provides support for HTTP2. In this workshop, learn how to create a web server utilizing Hyper under the hood and boost the performance for your web apps.
React Summit 2022React Summit 2022
164 min
GraphQL - From Zero to Hero in 3 hours
How to build a fullstack GraphQL application (Postgres + NestJs + React) in the shortest time possible.
All beginnings are hard. Even harder than choosing the technology is often developing a suitable architecture. Especially when it comes to GraphQL.
In this workshop, you will get a variety of best practices that you would normally have to work through over a number of projects - all in just three hours.
If you've always wanted to participate in a hackathon to get something up and running in the shortest amount of time - then take an active part in this workshop, and participate in the thought processes of the trainer.
TestJS Summit 2023TestJS Summit 2023
78 min
Mastering Node.js Test Runner
Node.js test runner is modern, fast, and doesn't require additional libraries, but understanding and using it well can be tricky. You will learn how to use Node.js test runner to its full potential. We'll show you how it compares to other tools, how to set it up, and how to run your tests effectively. During the workshop, we'll do exercises to help you get comfortable with filtering, using native assertions, running tests in parallel, using CLI, and more. We'll also talk about working with TypeScript, making custom reports, and code coverage.