Skip to content
Home » How to Use Setter Method in JavaScript to Assign Values to an Object Property – Program to Check and Set Age

How to Use Setter Method in JavaScript to Assign Values to an Object Property – Program to Check and Set Age

In this blog post, we will learn how to use the setter method in JavaScript to assign values to an object property. We will specifically look at an example program that checks and sets the age of a person. This tutorial is targeted towards students and working professionals who want to enhance their understanding of JavaScript and learn how to use setter methods effectively.

Code

const person = {
  _age: 37,
  set age(newAge){
    if (typeof newAge === 'number'){
      this._age = newAge;
    } else {
      console.log('You must assign a number to age');
    }
  }
};

/*
We can perform a check for what value is being assigned to this._age.
When we use the setter method, only values that are numbers will reassign this._age
There are different outputs depending on what values are used to reassign this._age.

*/

Code Explanation

In the provided code snippet, we have a JavaScript object named “person” with a property called “_age” and a setter method named “age” to set the value of “_age”. Let’s dive into the code explanation step by step.

1. The object “person” has an initial age value of 37, stored in the property “_age”.

2. The setter method “age” is defined using the “set” keyword, followed by the method name “age” and a parameter “newAge”.

3. Inside the setter method, we perform a check to ensure that the value being assigned to “this._age” is a number. If the condition is true, the value is assigned to “_age”. However, if the condition is false, an error message is logged to the console.

4. The program provides an example of how the setter method handles different inputs. When we use the setter method to assign a value to “person._age”, only numbers will be accepted. If a non-number value is passed, an error message will be displayed.

Here is an example usage of the code snippet:


person.age = 25; // Assigns the value 25 to person._age
console.log(person._age); // Output: 25

person.age = “twenty-five”; // Assigns a non-number value

// Output: You must assign a number to age

console.log(person._age); // Output: 25 (unchanged)

Conclusion

In this blog post, we explored how to use the setter method in JavaScript to assign values to an object property. We learned about the code snippet that checks and sets the age of a person, providing different outputs based on the input value. By understanding and using setter methods effectively, we can apply the necessary validations and constraints to our code, ensuring data integrity. This knowledge will be valuable for students and working professionals looking to enhance their JavaScript skills and develop robust programs.

Also checkout the following codes.


Program to Create a Robot Factory and Make It Beep
How to Optimize Conditional Statements in JavaScript