MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Getters, Setters & Static Methods0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
2.1Class Syntax & Constructor Methods2.2Getters, Setters & Static Methods2.3Private Fields & Methods (#field)2.4Prototypal Inheritance vs Class Inheritance2.5Mixins & Object Composition
HomeTutorialsJavaScript for AdvancedGetters, Setters & Static Methods
Private Fields & Methods (#field)

Getters, Setters & Static Methods

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

Modern JavaScript classes provide accessor properties (get and set) for computed values and data validation, alongside static methods and properties that belong to the class constructor itself rather than to individual instances.


1. Accessor Properties: Getters and Setters

Getters and setters bind an object property to a function that is invoked whenever that property is read or assigned. They resemble standard properties syntactically while providing encapsulation:

1class Temperature {
2 constructor(celsius) {
3 this._celsius = celsius; // Internal convention
4 }
5 
6 // Getter: Invoked when reading temp.celsius
7 get celsius() {
8 return this._celsius;
9 }
10 
11 // Setter: Invoked when assigning temp.celsius = value
12 set celsius(value) {
13 if (typeof value !== 'number') {
14 throw new TypeError('Temperature must be a numerical value.');
15 }
16 if (value < -273.15) {
17 throw new RangeError('Temperature cannot be below Absolute Zero (-273.15°C).');
18 }
19 this._celsius = value;
20 }
21 
22 // Computed Getter: Fahrenheit
23 get fahrenheit() {
24 return (this._celsius * 9) / 5 + 32;
25 }
26 
27 // Setter for Fahrenheit updates internal Celsius!
28 set fahrenheit(val) {
29 this.celsius = ((val - 32) * 5) / 9;
30 }
31}
32 
33const weather = new Temperature(25);
34console.log(weather.celsius); // 25
35console.log(weather.fahrenheit); // 77
36 
37weather.fahrenheit = 212; // Updates via setter
38console.log(weather.celsius); // 100

2. Static Methods and Properties

The static keyword defines methods and properties that exist on the class constructor itself, not on instance objects. They are ideal for utility functions, factory methods, or shared constants:

1class User {
2 // Static Property (ES2022)
3 static MIN_PASSWORD_LENGTH = 8;
4 
5 constructor(username, email) {
6 this.username = username;
7 this.email = email;
8 }
9 
10 // Static Factory Method: Constructs User from JSON API response
11 static fromJSON(jsonString) {
12 const data = JSON.parse(jsonString);
13 return new User(data.name, data.email);
14 }
15 
16 // Static Utility: Compares two users
17 static areEqual(userA, userB) {
18 return userA.email.toLowerCase() === userB.email.toLowerCase();
19 }
20}
21 
22// Access static members via the Class name:
23console.log(User.MIN_PASSWORD_LENGTH); // 8
24 
25const user1 = User.fromJSON('{"name": "Elena", "email": "elena@example.com"}');
26console.log(user1.username); // "Elena"
27 
28// Instances CANNOT access static methods:
29// user1.fromJSON(...); // TypeError: user1.fromJSON is not a function

3. Static Inheritance

In JavaScript, static members are inherited through the prototype chain of constructor functions:

1class BaseService {
2 static getServiceName() {
3 return 'Base Core Service';
4 }
5}
6 
7class AuthService extends BaseService {}
8 
9console.log(AuthService.getServiceName()); // 'Base Core Service' (Inherited statically!)

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
How do you access a getter property defined as get fullName() on an instance user?
Question 2
What is the primary purpose of a setter method?
Question 3
Can an instance of a class directly call a static method (e.g. instance.myStaticMethod())?
Question 4
What common design pattern is frequently implemented using static class methods?
Question 5
What error occurs if a setter attempts to assign to the exact same property name without a backing field (e.g. set score(val) { this.score = val; })?
Next Lesson

Private Fields & Methods (#field)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Class Syntax & Constructor MethodsPrivate Fields & Methods (#field)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext