11 Bad Ways to Write JavaScript
<p>JavaScript is an integral part of modern web development, but it has some pitfalls to be aware of. In this article, we’ll introduce ten common ways to write JavaScript to help you avoid some common mistakes and pitfalls.</p>
<h1>1. Implicit type conversion</h1>
<p>Implicit type conversion refers to JavaScript automatically converting one data type to another data type at runtime. For example, when you concatenate two strings using the plus sign, JavaScript automatically converts them to strings and concatenates them. While this may seem convenient, it can often cause problems, especially for inexperienced developers.</p>
<pre>
let result = "3" + 4 + 5 ; // '345'
let result2 = 3 + 4 + "5" ; // '75'</pre>
<p>In the above code, the output result of the first line is the string ‘345’, and the output result of the second line is the string ‘75’. This is because JavaScript converts the number to a string when processing the plus sign, and then performs the concatenation operation. Therefore, when you use the plus sign to concatenate numbers and strings, care must be taken to avoid unexpected behavior.</p>
<p><a href="https://medium.com/@Choco23/11-bad-ways-to-write-javascript-fec3869632eb">Website</a></p>