Lodash - understanding the use of variables intead of a simple ternary operator to return a value

Lodash Project

hi everyone,

in several of the items in this project the suggestions tell us to declare variables to use as the return value, instead i chose to return the value from a ternary operator on line of code; to me this looks much cleaner and can’t really see the importance of the other approach.
example following Codecademy suggestion:

has(object, key){
let hasValue = object.hasOwnProperty(key);
return hasValue;

My implementation

has(object, key){
return object.hasOwnProperty(key) ? true : false;

can anyone illustrate this for me please?

I am not familiar with that project, so providing only my general two cents. I agree that in this scenario it is not that much different declaring a temporary variable vs just returning the function result.
You could even write your example in the following way to get it even shorter:

has(object, key){
return object.hasOwnProperty(key);

Still, in many cases temporary variables are ok and even encouraged if they make the code easier to read or the logic easier to follow.

Here the example is pretty self explanatory from the name of the method being called, but in other scenarios there may be some long expression or calculation that is being done. Or multiple parts being calculated that then get added up or returned together. So breaking apart the different complex pieces into easier to read variables, then using the simpler named variables for the final calculations or output make it very legible what your logic is doing without having to dive into the complexity of how that specific portion was calculated.

thanks for replying,

i agree with you when there is complexity and/or calculation in the code but in these one line code blocks, i really can’t see the purpose.

1 Like