5 bad habits of JS coding, how to avoid them?
Have you ever had this feeling when reading Javascript code You hardly understand the code role? The code uses a lot of Javascript tricks? Naming and coding style too arbitrary? These are all symptoms of bad coding habits. In this article, I describe 5 common bad coding habits in Javascript. Importantly, this article will give some actionable suggestions on how to get rid of these habits. 1. Do not use implicit type conversion Javascript is a A loosely typed language. When used correctly, this is a benefit because it gives you flexibility. Most operators + – * / == (excluding ===) perform implicit conversions when dealing with operands of different types. The statements if(condition) {…}, while(condition){…} implicitly convert the condition to a Boolean value. The following example relies on implicit conversion of types, which can sometimes be confusing: console.log (“2” + “1”); // => “21” console.log(“2” – “1”); // => 1 console.log('' == 0); // => true console.log(true == []); // -> false console.log(true == ![]); // -> false Relying too much on implicit type conversions is a bad habit. First, it makes your code less stable in edge cases. Second, there is an increased chance of introducing bugs that…