
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the difference between getter and setter in JavaScript?
Getter
When a property is accessed, the value gets through calling a function implicitly. The get keyword is used in JavaScript. An identifier, either a number or a string is allowed for set.
Setter
When a property is set, it implicitly calls a function and the value is passed as an argument. With that, the return value is set to the property itself. The set keyword is used in JavaScript. An identifier, either a number or a string is allowed for set.
Example
Here’s an example showing how to implement both getter and setter
<html> <body> <script> var department = { deptName: "Finance", deptZone: "South", deptID: 105, get details() { return "Department Details<br>" + "Name: " + this.deptName + " <br>Zone: " + this.deptZone + "<br>ID: " + this.deptID; }, set details(info) { var res = info.toString().split(' '); this.deptName = res[0] || ''; this.deptZone = res[1] || ''; this.deptID = res[2] || ''; } } department.details = 'Marketing North 001'; document.write(department.deptName); document.write(department.deptZone); document.write(department.deptID); </script> </body> </html>
- Related Articles
- What is the difference between getter/setter methods and constructor in Java?
- Getter and Setter in Python
- How to define getter and setter functions in JavaScript?
- Getter and Setter in Dart Programming
- Difference Between Constructor Injection and Setter Injection in Spring
- What is the difference between == and === in JavaScript?
- What is the difference between comments /*...*/ and /**...*/ in JavaScript?
- What is the difference between jQuery and JavaScript?
- What is the difference between Java and JavaScript?
- What is the difference between JavaScript and ECMAScript?
- What is the difference between JavaScript and C++?
- What is the difference between functions and methods in JavaScript?
- What is the difference between setTimeout() and setInterval() in JavaScript?
- What is the difference between null and undefined in JavaScript?
- What is the difference between Bower and npm in JavaScript?

Advertisements