How to create a JavaScript class in ES6 ? Last Updated : 16 Feb, 2023 Suggest changes Share Like Article Like Report A class describes the contents of objects belonging to it: it describes a set of data fields (called instance variables) and it defines the operations on those fields (called methods). In order words, it is also defined as the collection or a group of object that contains object data types along with some methods in it. You can create a JavaScript class by using a predefined keyword named class before the class name. We will show you some examples below to illustrate how it works. Let us have a look over the following syntax section which illustrates how to declare a class in JavaScript: Syntax: class class_name { // body of the class // Here methods and object data types could be defined... } Creating an object of a class can be accomplished by using the new keyword and calling the constructor of that class (at the time of object instantiation). A constructor is declared by using the predefined keyword constructor. Constructors can be of any type, such as default constructors and parameterized constructors. As we can see here, we use a constructor to initialize and declare a variable. However, there can only be one constructor per class, and it can be parameterized or defaulted and further note point would be that the constructor doesn't have any return type. Syntax: class name { constructor(a, b, c) { // Initialize the class variable } } Example 1: JavaScript class Geeks { // Constructor constructor(num1, num2) { console.log("Inside Constructor"); // Initialize class variable this.num2 = num2; this.num1 = num1; } } // Initialize the class object let obj = new Geeks(1, 2); console.log(obj.num1); console.log(obj.num2); Output: Inside Constructor 1 2 Example 2: JavaScript class Geeks { constructor(num1, num2) { this.num2 = num2; this.num1 = num1; } add() { console.log( this.num1 + "+" + this.num2 + "=" + (this.num1 + this.num2)); } } let obj = new Geeks(1, 2); obj.add(); Output: 1+2=3 D debadebaduttapanda7 Follow Article Tags : JavaScript Web Technologies ES6 JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like