I have code-smell suggestion to add declaration to below function.
Add the "let", "const" or "var" keyword to this declaration of "nullToEmpty" to make it explicit.
nullToEmpty = function (obj) { if(typeof obj == 'undefined'){ return null; }else{ return JSON.parse( JSON.stringify(obj).replace(/:null/gi, ':""') ); } }; I wonder what is the most appropriate declaration type for this type of a function, should I use const ?
const nullToEmpty = function (obj) { if(typeof obj == 'undefined'){ return null; }else{ return JSON.parse( JSON.stringify(obj).replace(/:null/gi, ':""') ); } };
varas a global variable,letas a local variable, andconstas a constant variable like DEFINE MACRO inc++. So in this situation, I'll usevar. But according to stackoverflow.com/a/33040926/15487427, all three types -var,let,const- of function declaration have got exceptions for some situation, and declaration asfunction nullToEmpty(obj)would be the best one. It was my little opinion :)constseems fine, if you're not going to have anothernullToEmptywithin the same function or top-level scope. Personally, I would just use a regularfunction, though, then you don't have to worry about calling it before it's defined. Also, you can just do likeif(obj === undefined)sinceobjis an argument. You should know thatnullis notundefined, though.typeof nullgives you'object'.if (!obj)instead ofif (typeof obj == 'undefined')in function. It will be suit for all types of boundary check(null,undefined, etc) and the code is the simplest.function nullToEmpty(obj) {…}declaration