# JS
* **Handling empty array, null or undefined**
*Do's*
- Can use _.isEmpty, _.isNull
```
if(!_.isEmpty(variable_name)){
// do task
}
```
*Don't*
```
if(variable_name !== null){
// do task
}
```
* **Use lodash instead of JS inbuilt methods**
*Do's*
- Accessing object responsibly - to avoid uncertain behaviour
```
let result = _.get(object, "firstChild.secondChild", defaultValue);
```
- Prefer filter over manual compare
```
_.filter(array, item => {
return !_.isNull(item);
});
```
- Avoid repeated method calls - use it with time consuming methods or query
```
_.debounce(func, time_before_accessible_again);
```
*Don't*
- Do not access objects directly
```
let result = object.firstChild.secondChild || defaultValue;
```
- Do not use map for filtering
```
_.map(array, item=>{
if(item != null){
return item;
}
})
```
* **Using map responsibly**
*Do's*
- Whole array traverse - map should not be used when you don't want whole array access
```
for(let index = 0; index < max_length; ++i){
if(array[index] == "desigred_value"){
break;
}
}
```
*Don't*
```
_.map(array, item =>{
return item == "desigred_value";
});
```
* **Cloning and comparing**
*Do's*
- cloning is required to remove value reference
```
let clonedValue = _.cloneDeep(value);
```
- isEqual compares key and data, key order does not matter
```
let areSimilar = _.isEqual(value, otherValue);
```
*Don't*
- Do not compare object by making strings, different key order will give false result
```
let valueString = JSON.stringify(value);
let otherValueString = JSON.stringify(otherValue);
let areSimilar = _.isEqual(valueString, otherValueString);
```
We are using lodash for all JS standard methods, as lodash take care of undefined and all other edge cases. To learn more see [lodash documentations](https://lodash.com/docs).