DEV Community

Cover image for JavaScript Concept Clear

Posted on

JavaScript Concept Clear


Call, Apply and Bind

The call () method invokes a function with a given ‘’this’’ value and arguments provided one by one. It’s a predefined method in JavaScript.

apply (): The apply () method is almost similar to the call () method. The only difference is that call () method takes arguments separately whereas, apply () method takes arguments as an array. So, apply () method Invokes the function to pass in arguments as an array.

bind (): This method returns a new function, allowing to pass in an array and any numbers of arguments, where the value of “this” keyword will be bound to the owner object, which is provided as a parameter.


Working process of Closure in JavaScript

A closure is an inner function that admits to the variables that belong to the outer that is enclosing the function’s scope chain. It gives to access an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created. To use a closure, simply define a function inside another function and expose it.

Closures provide a better, concise, creative, and expressive writing code for JavaScript developers and programmers. Technically speaking, closures are a combination of lexical environment and function.

In other words, a closure is a locally declared variable that is related to a function and stays in the memory when the related function has returned. The closure contains all local variables that were in-scope at the time of the closure creation. There are three ways in which the closure can access variables-
• variables in its scope
• global variables.
• variables in the enclosing function’s scope,


pop()method in JavaScript, The use of the Push method in JavaScript

The pop () method is similar to the shift () method, but the key difference is that the shift method works at the array’s start. On the other hand, the pop () method takes the last element of the given array and returns it. For example,

var joya = ["Shirt", "Pant", "Tshirt"];
//Now joya becomes Shirt, Pant

The push method is used to add or append one or more elements to an array end. Using this method, we can append multiple elements by passing multiple arguments.


The “this” keyword indicate in JavaScript

The “this” keyword in JavaScript refers to the object that it belongs to. This keyword has different values depending on where it is used. In a method, this keyword refers to the owner object and, in a function, this keyword refers to the global object.


Difference between Java and JavaScript


Java is an object-oriented programming language, on the other hand, JS is object-based scripting, interpreted language. Java code can work on any platform including the web browser. But JavaScript is written just for web browsers. Among other things, Java demands more memory. Java is very strongly typed and asks all variables to have a declared type. Another way, JS is lightweight and is weakly typed.


Event bubbling in JS Or how does event delegate work in JS?

Event bubbling is a way of event propagation in the HTML DOM API when an event occurs in an element inside another element, and both elements have registered a handle for that event. With event bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements. The execution starts from that event and goes to its parent element. Then the execution passes to its parent element and so on till the body element.


Hoisting in JavaScript.

Hoisting is the default behavior of JavaScript where all the variable and function declarations are moved on top. Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before the execution of the code. The scope can be both local and global. For Example:
hoistedVariable = 17;
console.log(hoistedVariable); // outputs 17 even when the variable is declared after it is initialized

var hoistedVariable;


The scope of JavaScript

Scope is the accessibility or visibility of variables, functions, and objects in some particular part of code during the run time. Earlier JavaScript had only Global Scope and Function Scop. But now JavaScript has a total of four types of scope. They are-
• Block scope: let and const provide block scope. Variables declared inside a { } block cannot be accessed from outside the block. Var keywords cannot have block scope.

• Local scope: Variables declared inside any function with var keyword are called local variables. Local variables are created when a function starts and deleted when the function is completed. Local variables cannot be accessed or modified outside the function declaration. Local variables have function scope.

• Function scope: JavaScript has a function scope and every function creates a new scope. In this scope, variables defined inside a function are not accessible from outside the function. The var, let and const are quite similar variables when declared inside a function. So, var, let, and const all have function scope.

• Global scope: If I declare variables outside of any function then this will be global variables. Global variables can be accessed and modified from any function. All scripts and functions on the web page can access it. Variables declared with var, let and const are quite similar when declared outside a block. They all, var, let and const have Global scope.
One important note is that- If I assign a value to a variable that has not been declared yet, it will automatically become a global variable.


Generator function in ES6

It is the new concept introduced in ES6. A generator function provides us with a new way to work with iterators and functions. The generator function is a special kind of function that may be paused in the middle either one or many times and can be resumed later. A generator function followed by an asterisk function keyword. The declaration of the asterisk function is used to define a generator function.

When the generator gets called, it does not run the code. Instead, it returns a special object, which we called a Generator object to manage the execution. Unlike the regular function, the generator function can return or yield to the multiple values, one after another, on the requirement.


API, Difference between Get vs post

API stands for Application Programming Interface which is used to connect for the communication and information exchange between two apps. API connects two devices or programs in order to facilitate the exchange of information between them.

The difference between Get & Post are given below-
Get Post
GET is used to request data from a specified resource like some API URL. POST is used to send data to a server to create or update a resource.
GET request is often cacheable. The POST request is hardly cacheable.
GET Parameters to remain in web browser history. Parameters are not saved in web browser history.
Get request is not secured because data is exposed in the URL bar. Post request is secured because data is not exposed in the URL bar
Get request is more efficient and used more than Post. Post request is less efficient and used less than get.

Top comments (0)