Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | Classes

  • Last Updated : 25 Nov, 2021

Introduction of new version of JavaScript (ES6) introduced the use of classes instead of functions.Classes are similar to functions.They use class keyword instead of function keyword. 
They use constructor method to initialise. 

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

class classname {
  constructor(parameter) {
    this.classname = parameter;
  }
}

Below example illustrates the JavaScript classes:

html




<!DOCTYPE html>
<html>
 
<body>
    <center>
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
         
<p>
          A Computer Science Portal for Geeks
        </p>
 
 
        <b>JavaScript Classes</b>
 
        <p id="demo"></p>
 
 
        <script>
            class class_name {
                constructor(value) {
                    this.classes = value;
                }
            }
 
            myvalue = new class_name("GeeksforGeeks");
 
            document.getElementById("demo").innerHTML =
              myvalue.classes;
        </script>
    </center>
</body>
 
</html>

Output: 
 

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!