HypeTeq

Subconscious error while dealing with JavaScript

Hey guys, I hope all are doing fine in this pandemic situation and following the necessary guidelines. This time we will learn about the errors we make subconsciously in our code while dealing with JavaScript. This kind of errors can be problem in long run of a coder because we are not necessarily aware about this kind of errors, which will reflect in out coding many of the times so without wasting much time, let’s just dive right into it.

1. Confusion while using equality operators

JavaScript do automatic type conversion when we use == so be careful to use == (or! =) operator you get might be wrong result. When we use === (or! ==) JavaScript compare without doing type conversion so you get always your desired results.
Expression Result
‘ ‘ == false
True
‘ ‘ === false
False
’10’ == 10
True
’10’ === 10
False
‘ ‘== 0
True
‘ ‘=== 0
False

2. Wrong / Incorrect Reference of this keyword

As you can see in above example if we try to access getMyName () method directly (in first image) using this keyword inside set Timeout function it’s give error of ‘undefiled is not a function’ it’s due to execution context scope. So, for solution we store ref of current context in some variable and after that we can use it for accessing the current context.

3. Confusing addition and concatenation

In the JavaScript Addition and concatenation both can be done by + operator. Addition is about adding numbers and concatenation is about adding string. So, need to be vary carful when using concatenation operator (+).in below example we doing simple adding two number function If we pass one argument as string JavaScript automatically done concatenation of both parameters.

4. Ignoring ‘var’ and Scope’s usagIgnoring ‘var’ and Scope’s usagee

The Difference between ‘var’ and ‘let’ is all about its scope of usage. ‘var’ has function scoped whereas ‘let’ has block scope. These two concepts always remember when you dealing with JavaScript otherwise it’s lead to bugs.in below example I will try to illustrates this discrepancy.

5. Callback hell (Assuming callbacks to be synchronous)

First of all, JavaScript callback are asynchronous. They are used to be called after an operation when some execution has completed. For example, setTimeout function has callback with it’s first argument and duration in millisecond as a second argument so:
After 3 seconds the callback function is called but before it completes with its execution the rest code runs and that is a reason why the callback console.log was run last. Now the mistakes happens when we use something that will be return in callback function and used on other operation this leads to big logical glitch this is demonstrate in following example with its solution.
There are many errors which are similar to the ones that I mentioned. These errors don’t mean that there is some problem with your coding knowledge. These errors just shows that while you are concentrating on bigger errors, these small ones slip through your mind. These errors are the ones that are tedious to identify but can be solved easily. So better be aware of these small things that can be a huge problem in long run.
That’s it for today.
Have a good one.
See Yaa!

Tagged:

SAY HELLO

Follow Us on Social Media

en_USEnglish