
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Class Keyword in JavaScript
JavaScript classes, introduced in ES6, are syntactical sugar over JavaScript prototype-based inheritance. Classes are in fact "special functions". You can define classes in JavaScript using the class keyword using the following syntax −
class Person { // Constructor for this class constructor(name) { this.name = name; } // an instance method on this class displayName() { console.log(this.name) } }
This is essentially equivalent to the following declaration −
let Person = function(name) { this.name = name; } Person.prototype.displayName = function() { console.log(this.name) }
This class can also be written as a class expressions. The above format is a class declaration. The following format is a class expression −
// Unnamed expression let Person = class { // Constructor for this class constructor(name) { this.name = name; } // an instance method on this class displayName() { console.log(this.name) } }
No matter how you define the classes as mentioned above, you can create objects of these classes using the following −
Example
let John = new Person("John"); John.displayName();
Output
John
You can read in depth about JS classes and the class keyword at https://www.tutorialspoint.com/es6/es6_classes.htm.
- Related Articles
- What is the “get” keyword before a function in a class - JavaScript?
- Super keyword in JavaScript?
- "extends" keyword in JavaScript?
- Explain JavaScript "this" keyword?
- The yield* expression/keyword in JavaScript.
- C++ Program to Show Use of This Keyword in Class
- Golang Program to Show Usage of Static keyword in Class
- Golang program to show use of super keyword in class
- What is the keyword used in instantiating a class in Java?
- What is the yield keyword in JavaScript?
- How to use void keyword in JavaScript?
- When should I use the keyword ‘this’ in a Java class?
- How does the “this” keyword work in JavaScript?
- How to use the debugger keyword in JavaScript?
- What is the 'new' keyword in JavaScript?

Advertisements