10 Javascript tricky things that you should know before considering yourself as a Javascript expert
Null vs Undefined
1.Undefined
Simply Undefined means the value you are looking for is not still defined. such as you console.log a value of any object that you had not defined earlier.
see the example:
const premik = {name:”smart dude”, phone:458421};
console.log(premik. gf);(here gf is not mentioned in permik object
2.Null
Simply the value of null should you explicitly defined or You have defined earlier but somehow it removed
Truthy vs False
3.Truthy
In javascript, there are some default value considered as truth such as
‘0’(zero, as a string but only zero is false)
‘ ‘(empty string)
,[](empty array)
4.Falsy
false(false keyword himself)
0,
“”,
undefined,
null,
NaN
5.Double vs triple equation
Simple double equation chacks the type of value but triple equation always checks the type of value. trick mater is their double equation change both values to same comparable value type but not the triple equation
const first = 0;
const second = false;
if(first == second){
console.log(“condition is true”);
}else{console.log(“condition is false”);}
6.Scope and block scope
When you declare any variable inside a function you can access it only from the inside of the function not outside of the function that is called scope or range of any value. One tricky thing is if you declare any valuable without var, let, or const then it’s considered as a global variable but it is not good practice. Another tricky thing is if you declare any variable using let or const the variable never hoist to parent but if you declare with var its value hoist to parent and can be used inside the function
7. Closure, encapsulation
As a junior developer simply remember that when a function is called several times from inside another function, it creates a closed environment and keeps a reference value for every call.
8.Difference between bind, call and apply
Suppose you have a method inside the object and you want to reuse this method with another object then you have three option bind, call and apply
Bind takes the previous object’s method and binds with new object’s as a method. see the example below
const newmethod= old object.old function.bind(new object);
The call does not create a new object its directly uses the previous method. see the example below.
previous object.method.call(newObject, 900, 100, 10);
The apply is similar to call but different in applying process, see the example below
the previous object. method.call(newObject,[ 900, 100, 10]);
9.how to understand the “This” keyword
The simple value of THIS is determined by whose context function is running and it can be easy to know identifying who is left side of it. if there is nothing on the left side the context will be a window(global).so THIS keyword runs based on who is on the left side.
10.How Javascript works event loop stack and queue
Simply in Javascript event loop have 3 part. When js runs its separates all tasks into 3 parts.
1. Stack(primarily JS read and stack the tasks and do one by one)
2.Queue(if the stack is over then call from queue list and enroll as a stack)
3.Heap(a list that is not enrolled in the queue and waiting to enroll in the queue)
const newmethod= old object.old function.bind(new object);