Setter and getters problem


Why isnt line 24 executing? ( The else statment in set Age()Cant figure i out :frowning:

class Player {

    constructor(name, age, position) {

    this._name = name;

    this._age = age;

    this._position = position;


get name() {

    return this._name;


get age() {

    if(typeof this._age === "number") {

        return this._age;


        return "Cant fetch players age"



get position() {

    return this._position;


set age(value) {

    if(typeof value === "number") {

        return this._age;

    } else {

        return "Age must be a number";




const test = new Player("Peter", "38", "Forward")


BR/ Peter

Because you don’t use the setter in the constructor. You set the underlying property directly.

So i put the Set statment inside the constructor brackets?

Yes. However, having a second look at your code, the setter should have void signature (no return). you should set the this._age property in the setter

1 Like

Thank you! This solved it. :slight_smile: