Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Classes And Their Instances

What are classes?

Classes are a template/blueprint for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes.

In Modern Javascript, classes act as special functions.

 

Class Declaration

To declare a class we can use a special keyword class followed by class name and then pair of curly braces which wraps the body of the class.

class Student{

//Body of class

}

One can also declare a class as:

const Student = class {
// Body of class
};

Instance of a class

An instance is an object containing data and behavior described by the class.

We can create an instance of a class (initiate the class), by using new keyword.

var student1 = new Student();  //student1 is now an instance of class Student

We can initialise the data that an instance may contain by defining a constructor function in the class body which is called whenever a class instance is created.

class Student{
constructor(name, age) {
this.name = name,
this.age = age
}
}

const student1 = new Student("max", 20)
console.log(student1.name) //prints max
console.log(student1.age) //prints 20

 

static 

The static keyword defines a static method or property for a class. Neither static methods nor static properties can be called on instances of the class. Instead, they’re called on the class itself. Static methods are often utility functions.

We dont need to create any instance to call or access the static members.

class A{
    static propA = "propA";
    static methodA() {
        console.log("methodA");
    }
}

console.log(A.propA); //prints "propA"
A.methodA();  //prints "methodA"
const a1 = new A(); //creating an instance of class A
console.log(a1.propA); //prints undefined (propA is not a member of instance a1)
a1.methodA(); //gives an error

 

Try the following example in the editor below.

For the given class Employee, define a constructor function so that we can initialise the name, position and skill properties with default input strings while creating instances of the class.

Sample Input

 Max
 SDE1
 C++

 

Start solving Classes And Their Instances on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test