DEV Community

Discussion on: Which would you prefer and why?

Collapse
 
defman profile image
Sergey Kislyakov

The second example looks a lot nicer to me.

Note 1: you can use const instead of let in your second example because you do not transform the object anywhere in your function.

Note 2: second example will return undefined instead of '' if the key in the object does not exist. I think you'd handle that as well.

Collapse
 
shalvah profile image
Shalvah

*because you do not reassign the reference.

You can transform an object variable declared with const.

Collapse
 
lozadaomr profile image
Omar Lozada

Thanks for pointing out the difference between let and const, I'll keep that in mind.