Using return or console.log to return string when dealing with methods?

I’m learning about using setters and getters to get and reassign properties in objects.
This is the lesson:

I’m confused about when it’s appropriate to use return or console.log to return a string when using a conditional statement. get numOfSensors is using a return keyword to return string but set numOfSensors is using a console.log.

I tried using a return on set numOfSensors else statement, but it doesn’t have the intended effect.

robot.numOfSensors is supposed to change the value of numOfSensors however when using the return on the set method’s else statement, assigning robot.numOfSensors with a string instead of a number returns a number still when it is supposed to return a string. But robot.numOfSensors works when using console.log not return. I want to understand why that is.

const robot = { _model: '1E78V2', _energyLevel: 100, _numOfSensors: 15, get numOfSensors(){ if(typeof this._numOfSensors === 'number'){ return this._numOfSensors; } else { return 'Sensors are currently down.' } }, set numOfSensors(num){ if(typeof num === "number" && num >= 0){ return this._numOfSensors = num; } else { console.log("Pass in a number that is greater than or equal to 0"); } } }; robot.numOfSensors = 100; console.log(robot.numOfSensors);

In simple terms, a function returns something if you can set the result of the function to a variable. It makes perfectly good sense to return something from a getter because you are trying to “get” a value. You want the value because you are going to do something with it. You can set it to a variable.

A setter function is designed to change the value of something that already exists. We don’t need to receive any information back from a setter function for it to work properly. Setting the number of sensors to 30 doesn’t require any info coming back to us. It just gets set to 30. The console.log() just acknowledges that the sensor value was changed.

The main issue with your code at the end of the script is that returning a string and logging a string are two very different things. Logging a string just prints it to the console, and then it’s gone. Returning a string actually gives you a value that you can do stuff with. By itself, it won’t print to the console. But you could if you wanted to. Or you could pass it to another function, write it to a file, send it over the internet, etc.