I have a colleague that instantiates all his variables with empty string for their default value. That variable later could have an array or object assigned to it an although that might be known he still assigns empty string as default. Ex.
function getAllCars(){
var listOfCars = ['bmw','fiat','reno'];
return listOfCars;
}
function getAnyCar(){
var cars = "";
cars = getAllCars();
var car = cars.slice(0,1);
return car;
}
TL;DR:
What should a good practice for defining cars be
var cars;var cars = [];var cars = "";
I am trying to explain to him that if you know that the variable cars is going to be used to hold an array you should define it with an empty array.
He argues that he knows that variable is 100% going to be assigned an array down the line i.e cars = getAllCars(); so it doesn't matter. In a small function such a thing may be visible but I believe it to be bad practice.
const cars = getAllCars(). Dealing with unclean state is pretty hard. This is subjective though.