Let’s dive into the list of mistakes directly.
1. Falling in the callback hell
Simple callback example:
So, in the above function call to “updateCustomer” updates customer information in the database. In order to return this updated information we call the callback function at the end of “updateCustomer” and pass it the results as the parameter.
But, if not handled properly this method of using callback functions can get real messy, real soon.
What happens when there are multiple functions which depend on the results returned by the other function?
Usage of such functions will look something like this:
2. Misunderstanding “===” operator
To make it real simple, keep this in mind.
”===” behaves just like “==” operator. Except, “===” also compares the types.
Let’s see an example:
In the above example, “==” operator just did a value comparison. Meaning, it implicitly converted necessary types of operands and compared their equality.
But, as the data types of 1 and “1” are not same, “===” returned false. Because “===” operator does not do any type conversion for you. It strictly checks if the type of operands is same and whether their values are equal as well.
3. Missing semicolons:
But, my suggestion would be- don’t fall into this trap. It might backfire on you.
Consider this classic example:
4. Forgetting that variables are not block scoped
To understand function scope, follow the below example:
In the above “foo” function, I have written an if-block. In that if-block I have declared a variable called “y”. And outside the if-block, I am accessing the variable “y” to display its value.
If you try to do something like this in Java or C++, your compiler will through an error. That is because they are block-scoped language. Therefore, in such languages you cannot access the variable outside the block of code it is declared in.
5. Confusing Addition and Concatenation
There is a common operator used to add numbers and concatenate strings. That is “+”.
When you use “+” operator with two same types, you are safe. Let’s checkout a code snippet:
Above code behaves as we expect. But, the problem occurs when you try to use “+” operator with mixed types.
There is a simple trick to get around the above problem. Let’s see it in action:
Above mistakes may seem silly and trivial, but when you are working on a complex application it becomes tough to track them down.