Things you should know about javascript

Things you should know about javascript

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).

Javascript Truthy and Falsy

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.

null data type

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

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

undefined data type

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

undefined data from function

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

undefined data from function

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

undefined data from object

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

undefined data from array

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

explicitly assigned undefined data

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.

condition check using double equal (==)

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

condition check using triple equal (===)

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.

global scope

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.

block scope

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.

Closure, encapsulation and private variable

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.

bind method

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.

call method

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.

apply method

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.

variable on local scope

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.

variable in global scope

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

Automatically Global variable

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 .

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

this keyword in object

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

this keyword in global function

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.

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.

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.

setInterval function
Javascript event loop

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.

Functions in call stack

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.

Functions in call stack and message queue

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store