Class in Node.js using ES6
The next method is using ES6. With the new Javascript standard ES6, we got a nice and clear syntax for creating classes. JavaScript classes are merely "syntactic sugar" over JavaScript's existing prototype-based inheritance to simplify the code for developers. Let's see how to perform the above operation using ES6.
Code
// Student class declaration
class Student {
constructor() {
this.id = "ID_01";
}
set name(name) {
this._name = name;
}
get name() {
return this._name;
}
sayHi() {
console.log("Hi, " + this.name + ", your ID is " + this.id);
}
}
var newStudent = new Student();
newStudent.name = "Sapna";
newStudent.sayHi();

You can also try this code with Online Javascript Compiler
Run Code
Output:
Hi, Sapna, your ID is ID_01
As you can notice, the syntax in ES6 is easier to read and understand. Also, ES6 allows you to extend classes.
We can also declare the above class as an expression like shown below.
// Person class expression
let Person = class {
}
Drawbacks of using ES6
One of the main drawbacks is that ES6 is not available on the client-side because most browsers don't support ES6. Due to this, classes on the server and client are different.
The other "drawback" is that you have to run your code in strict mode, forcing you to code more securely and with better performance.
Strict Mode
Strict mode enforces some strict principles for a JavaScript program. It means that the leverages given by JavaScript are not available while working in the strict mode. Using strict mode will make the code faster because the compiler can make better optimizations.
For example, if a variable is initialized with some value without declaring the variable, the JavaScript compiler will not throw an error and ignore it. But when strict mode is used, an error will be thrown.
Strict mode can be enabled in two ways:-
1. Starting the entire application in strict mode by starting node with
node —-use_strict webtential.js
2. Including the below code at the top of the file
"use strict";
Frequently Asked Questions
What is ES6?
ES6 or ECMAScript 2015 is the 6th version of the ECMAScript programming language standard. ECMAScript is the standardization of Javascript, which was released in 2015, and subsequently renamed ECMAScript 2015.
What is the difference between ES6 and JavaScript?
ES6 is the 6th version of ECMAScript, which has enhancements like class declarations, promises for asynchronous programming, lexical block scoping, destructuring patterns, proper tail calls, etc.
JavaScript is a prototype-based, multi-paradigm scripting language that is dynamic and supports object-oriented, imperative, and functional programming styles.
What is a class?
Class is a template used to create objects. They encapsulate data with code and work on that data.
Conclusion
This blog covered the basic concepts of class in Node.js. We explored the two methods to use a class in Node.js: using JavaScript prototype and ES6. We also discussed the drawbacks of using ES6.
Don't stop here. Check out Top 10 NodeJS Frameworks In 2021. If you are new to Node.js, check out the blog Introduction to Node.js.
Recommended Reading:
Four Pillars of OOPS
Also, check out this amazing course on Full Stack Web Development in Node JS along with this Guided Path for Basics of JavaScript on Coding Ninjas Studio.
Happy Learning!!