Classes and Objects in JavaScript
Last Updated :
28 Apr, 2025
Classes
Classes were first introduced in the new version of the ES6 classes which replaced the previously used functions. Class is nothing but a blueprint for an object of it. It is used to create an object mainly. If we relate it to a real-life example then it is like a plan for a building or house where that plan contains details about doors, windows, floors, etc. Based on the class which is the blueprint an object is made which can be referred to as a house in this example. So one plan is used to make a lot of houses in the same way one class can be used to create a lot of classes. So class is not a real-life entity but the object is one.
The Constructor Method
The constructor method is a special method in JavaScript with the exact name 'constructor.' It is automatically executed when a new object is created from a class. Its primary purpose is to initialize object properties, allowing you to define the initial state of an object during its instantiation.
Creating JavaScript Class
To create a JavaScript class, we must follow the following syntax.
Syntax:
// creating a class
class Name {
constructor(var) {
this.var = var;
}
}
JavaScript Class Methods
Defining class methods in JavaScript is easy and simple, we just need to add () following a method name.
Syntax:
class Name {
constructor(var) {
this.var = var;
}
// defining method
method() {
//Code Here
}
}
Class Getters and Setters
We can use getter and setter methods to get the value of an object and set the value of an object. We can use the get keyword for the getter method and the set keyword for the setter methods.
Syntax:
class Name {
constructor(var) {
this.var = var;
}
// defining getter method
get method() {
//Code Here
}
// defining setter method
set method(value) {
this.var = value;
}
}
Example: The code below demonstrates the creation and different implementations of JavaScript Classes.
JavaScript
class OOPs {
constructor(name) {
this.name = name;
}
// Getter method
get langName() {
return this.name;
}
// Setter method
set langName(x) {
this.name = x;
}
hello(){
console.log(`Hello ${this.name}`)
}
}
let obj = new OOPs('JavaScript');
console.log(obj.name);
obj.langName = 'Java';
console.log(obj.name);
Output:
JavaScript
Java
Object
Apart from the 7 primitive datatypes there is the most important datatype in JavaScript which is Object. Although the nature of this datatype is completely different from the primitive datatypes. That means in the primitive datatypes only one value is stored but an object can store more than one value even of different types.
Object Creation
Here is the syntax to declare an object with the name object_name and having the members inside it having key-value pairs and all the members are enclosed inside {}.
Syntax:
const object_name = {
key_1: value_1,
key_2: value_2,
...
}
We can also define it in a single line but this way defining increases readability.
JavaScript Object Properties
In JavaScript the members inside the object which are the key: values are called Object properties. For example, in the above-given syntax key_1: value_1 and key_2: value_2 are properties of the object.
To Access Object Properties:
1. Using dot Notation:
Syntax:
object_name.key_1
2. Using bracket Notation:
Syntax:
object_name["key_1"]
JavaScript Nested Objects: In this case, an object contains another object inside it.
Syntax:
const object_name-1 = {
key_1: value_1,
key_2: value_2,
const object_name-2 = {
key_3: value_3,
key_4: value_4,
}
}
JavaScript Object Methods: In JavaScript, we can add methods to Objects.
Syntax:
const object_name-1 = {
method_name: function
() {
//code here
}
}
Example: In the given example we can see how we can apply Javascript nested objects and also use the different accessing methods.
JavaScript
const GFG = {
topic: 'OOPs',
lang: 'JavaScript',
sub_topics: {
topic_1: 'Class',
topic_2: 'Object'
}
}
console.log(GFG.topic);
console.log(GFG.sub_topics.topic_1);
console.log(GFG["lang"]);
console.log(GFG.sub_topics["topic_2"]);
Output:
OOPs
Class
JavaScript
Object
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read