Open In App
Related Articles

Classes In JavaScript

Improve Article
Improve
Save Article
Save
Like Article
Like

JavaScript Classes are basically a blueprint or template of the object. JavaScript classes can be used to create new objects 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.

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

class emp {
    constructor(name, age) {
        this.name = name;
        this.age = age;

    }
}
const emp1 = new emp("Geek1", "25 years");
console.log(emp1.name);
console.log(emp1.age);

Output

Geek1
25 years

Using a Class in Javascript

We can use a JavaScript Class to create a JavaScript object.

Example:

Javascript

class emp {
    constructor(name, age) {
        this.name = name;
        this.age = age;

    }
}
const emp1 = new emp("Geek1", "25 years");
const emp2 = new emp("Geeks2", "32 years")
console.log(emp1.name + " : " + emp1.age);
console.log(emp2.name + " : " + emp2.age);

Output

Geek1 : 25 years
Geeks2 : 32 years
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 Nov, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials