Most common javascript interview question.

Null Vs Undefined

In javascript Null and Undefined little bit confusing. So, today I will clear the thing. In javascript null means the variable value has empty or you set the value null. When you declare a variable, if the variable value is empty It will show null. In javascript undefined means, you declare a variable but you do not the set value or pass the value, It will show undefined.

Note: null is only used when You expected you get an empty value or get a zero.

Double equal vs Triple equal

When you use a double equal in a condition its only check the value. If the value is equal (equivalent)it will show true. On the other hand Triple equal in a condition check the value and check the type. if value or type is not equal then it is thrown false.

Note: It is recommended to use triple equal to reduce bug in your code.

Scope and block scope

When you declare a variable Its create a scope. And It is the global scope. You can access this variable in any function. But if you declare a variable in a curly brace {} or a function, It creates a block scope. You can only access this variable in the block scope. let's have an example.

This keyword

In javascript, this keyword confuses a lot of developers. This keyword references the objects that are executing the current function. If the function is the method in an object this references that object itself. Otherwise, if the function is a regular function, which means it's not a part of an object, This references global objects.

In this example we can see this keyword in a regular function it references global objects. But in strict mode this keyword reference undefined. What if we using a constructor function instead of a regular function. And call this function with a new keyword.

Let’s recap by dealing with a regular function this keyword reference global object. But in a constructor function this reference new objects.

Closure

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

In this example, we create an add function inside this add function we return another function. This function increases the value. Note that the inside has no local variables of its own. However, since inner functions have access to the variables of outer functions, the inside function can access the variable declared in the parent function, .

An enthusiast JavaScript and React developer.