Things you should know about javascript

Things you should know about javascript

1.What are Javascript Truthy and Falsy values?

While checking any condition, Javascript takes some specific values as true or false by default. If the value we are checking is a number and if its value is 0, it takes it as falsy value, and if its value is anything other than 0 like 1,2,3, etc it is considered truthy. If the value to be checked is a string, an empty string(“”) is considered to be false. Anything inside the string other than that, like “ ”, “false”,”0” is considered truthy. All values are truthy except they are defined as falsy (i.e., false, 0, -0, 0n, “”, null, undefined, and NaN).

2.Difference between undefined and null data type, different ways to get undefined?

If a value of a variable is either not declared or initialized, then the value will be undefined. But if we set a value of a variable to null intentionally only then its value will be null. Null means intentionally missing value.

we can get the value of a variable undefined in many ways.

· if we don’t initialize a value in the first place.

· If it’s a function but we don’t return anything from that function.

· if it’s a function but when calling that function we don’t pass any required parameter.

· if it's an object but the property we want does not exist in that object.

· if it's an array but the element we want does not belong to that array.

· if we explicitly initialize a value of undefined.Though it's not recommended.

3. what are double equal (==) vs triple equal (===), implicit conversion?

When checking any condition if we put double equal between two compared values, it will return true if both variables have the same values it will not check the data type.

But when checking the value of two variables, if we put triple equal (===) between them it will check both value and data type.

4.Define Scope and block scope.

When we declare a variable using var inside a scope, it gets hoisted on the top of that scope. Its means it creates a global scope and it will be available from within and outside that function. But the variable value won’t get hoisted only the declaration part does.

When we declare a variable using let or const, it creates a scope level variable. That variable will only be available within that scope. It won’t get hoisted on the top of that scope.

5.Define Closure, encapsulation, and private variable.

When a function is called or returned from or inside a parent function, then the child function creates an enclosed/encapsulated environment and if he uses any variable of parent function, it will keep a reference of that variable.

6. Difference between bind, call and apply.

If we want to use a method of an object with another object that does not have that method inside itself, we can initially use the bind, call or apply method.

Bind: Bind will only bind the method to the new object but won’t change or affect its original parent object.

Call: Call is also used to use a method of one object with another object. It doesn’t require assigning the call method to any variable, we can call it directly by passing the parameters separated by a comma.

Apply: Apply is quite similar to the call method. The only difference is that when calling the method we have to pass the required in an array.

7. Define window, global variable, global scope.

Variable accessibility is determined by scopes. Scope is created with the creation of function. Variable declared within one function will have local scope within that function, which means it won’t be available from outside that function.

If a variable is not defined within any function it creates a global scope, which means it will be accessible from any function of that program.

If we don’t declare a variable while initializing a value inside a variable, it will also create a global scope.

Window is the global variable of the browser, it’s the execution environment of JavaScript. All variables or functions having global scope can be accessed through window .

8. How to understand this keyword?

Javascript this keyword determines the object context on which a function, method, or variable is executed.

this alone refers to the global object. In a function, this refers to the global object.

When working with any event this refers to the environment of that event. In call or apply method this refers to the object it is called with. In strict mode, this means undefined.

9. Asynchronous Javascript setTimeout, setInterval.

Javascript works synchronously by default. But if we want to achieve asynchronous functionalities, we can do it in three ways. By using async/await, promise and call back.
By using setTimeout method we can achieve asynchronous behavior in javascript.setTimeouttakes to arguments. the first one is a call back function, the second argument is the time after which the callback function will be executed. the call back function can be defined outside or inside the setTimeout function.

In this example, setTimeout function will be executed after all the codes are executed and at least after 5 seconds.
setInterval functions set a function to be executed after a specific time interval. It takes two arguments which are callback function and interval time.

10.How Javascript event loop works?

The event loop is an important concept to understand how javascript handles asynchronous functions being a single-threaded language. The event loop has three main regions call stack, message queue, and heap. call stack is where all the functions get stacked one after another and the last one gets executed first.

In this example all these functions on call stack like this-
the first( ) will be executed,then the second( ) and finally third( ) will be executed.
If javascript finds any asynchronous function, it will go to the message queue one after another. After all the functions in the call stack will be executed the event loop will take functions from message queue starting from the first one and execute them.

In this example third( ) and second( ) will go to the stack first.but setTimeout function will go to the message queue. After the call stack will be clear the setTimeout function will be thrown from message queue to stack and it will be executed.
Heap represents a large region where the memory allocation of objects happens.