I'm working with an API call using axios and getting back some JSON in a response. I have a variable that will hold the result of one of the JSON object's values, in this case a string:
let first_name = "";
//later on..
first_name = response.data.firstName;
I had my hand slapped because I had initialized first_name
to an empty string instead of null
and I'm not sure why-- the person doing the code review muttered something about best practices and didn't really answer me.
My question-- if I'm checking for an empty string instead of null
when using first_name
later on, does it matter what I initialized it to? Is there a javascript best practice or optimization I'm missing out on by setting the variable to an empty string?
[edit]
Some good discussion in the comments about how I'm using first_name
later on in the code. Let me elaborate.
I don't want to do a code dump, so let me put it this way, after first_name
has been assigned I check to make sure it's neither an empty string or null before using it. What I'm interested in here is whether what I did is wrong or inefficient in javascript terms, or 'worse' than assigning it as a null. I assigned it as a string as a mnemonic that it should be a string value if all goes well.