0

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, ':""') ); } }; 
5
  • 1
    if you are compiling it for use in older browsers use const as the variable should never change. always use const when something should not be changed. let when it will or can change. var not any more unless you are not compiling. Commented Aug 23, 2021 at 0:40
  • In my own rule for js coding, I always use var as a global variable, let as a local variable, and const as a constant variable like DEFINE MACRO in c++. So in this situation, I'll use var. 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 as function nullToEmpty(obj) would be the best one. It was my little opinion :) Commented Aug 23, 2021 at 0:51
  • const seems fine, if you're not going to have another nullToEmpty within the same function or top-level scope. Personally, I would just use a regular function, though, then you don't have to worry about calling it before it's defined. Also, you can just do like if(obj === undefined) since obj is an argument. You should know that null is not undefined, though. typeof null gives you 'object'. Commented Aug 23, 2021 at 0:52
  • p.s. I recommend to use if (!obj) instead of if (typeof obj == 'undefined') in function. It will be suit for all types of boundary check(null, undefined, etc) and the code is the simplest. Commented Aug 23, 2021 at 0:56
  • Just write a function nullToEmpty(obj) {…} declaration Commented Aug 23, 2021 at 1:21

1 Answer 1

2

var

var is the "old way" of declaring variables - it can be redeclared, and its scope is local ( only exists inside the current function ) if declared inside a function, and global otherwise. It might be useful in some cases, but it's not that recommended anymore since the global scope may cause some undesired effects if not used with caution.

let

let is a newer declaration, and has almost the same behaviour as var, but the scope is always local.

const

const is also always local, but it differs because it cannot be redeclared. That is a little bit misleading sometimes, though, because "you can't redaclare" does not mean "you can't alter it".

That works as expected for primitive values ( strings, numbers, etc ), but not for objects, arrays or functions.

This is invalid code:

const primitive = 1; primitive = 2; 

This is valid:

const array = [1, 2, 3]; array.push(4); 

answer

So, with all that said: yes, I would use const in that scenario. Your function declaration is not going to change, and there is no need for it to be of a global scope.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.