Wednesday, October 22, 2025
HomeLanguagesJavascriptClasses In JavaScript

Classes In JavaScript

The new version of JavaScript (ES6) introduced the use of classes instead of functions. Prior to ES6, there were only classes and, functions which are callable objects. A class in javascript is basically a blueprint or template of the object. New objects can be created from a class. 

Class In JavaScript

Classes are similar to functions. Here, a class keyword is used instead of a function keyword. Unlike functions classes in JavaScript are not hoisted. The constructor method is used to initialize. The class name is user-defined.

Syntax:

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

Example: The below example illustrates the JavaScript classes.

Javascript




<script>
    class emp {
        constructor(name, age) {
            this.name = name;
            this.age = age;
             
        }
    }
    const emp1 = new emp("Geek1", "25 years");
    document.write(emp1.name);
    document.write(":");
    document.write(emp1.age);
     
</script>


Output:

Geek1:25 years
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS