DEV Community

SHUBHAM GUPTA
SHUBHAM GUPTA

Posted on

Some popular JS topics

This blog post is mainly beneficial for freshers who started interviews. As a Fresher, I know its very difficult to face interviews and answer the questions.

Here, I mentioned some popular topics with proper definition which is useful during your interview preparation.

Closure-

A function bundle with its lexical environment forms the closure.

In other words, a Closure gives you access to outer function scope from an inner function. In JS, closures are created everytime a function is created at function creation time.

function x(){
var a =7;
function y(){
console.log(a);
}
y();
}
x();

//output:- 7
Enter fullscreen mode Exit fullscreen mode

Whenever function is returned, even if its vanished in execution context but still it remember the reference it was pointing too.

Uses Of Closure:

  • Module design Pattern

  • Currying

  • Memoize

Hoisting-

Hoisting in JavaScript is a behavior in which a function or a variable can be used before declaration.

Hoisting is a behavior in JavaScript where variable and function declarations are moved to the top of their scope.

In JavaScript, variable declarations are moved to the top of their scope (either the global scope or a function scope) during the compilation phase. This means that you can reference a variable before it is actually declared in your code.

getName();
console.log(x);
var x=7;
function getName(){
console.log("Hello!");
}
//output:
//Hello!
//undefined
Enter fullscreen mode Exit fullscreen mode

Event Loop-

Event Loop in JS is a mechanism through which the 'calls waiting for execution' in the callback queue/job queue can be put on the call stack. For any event from the callback queue/job queue to come to call stack, the call stack will have to be empty.

DOM-

The Document Object Model(DOM) is a programming interface for HTML and XML documents. It represents the structure of a document as a tree-like hierarchy of nodes, allowing programs to access and manipulate the content and structure of a document. The DOM can be used with any programming language, such as JS to make dynamic changes to a webpage.

Function Borrowing-

Function Borrowing allows us to use the methods of one object on a different object without having to make a copy of that method and maintain it in two separate places. It is accomplished through the use of call(), apply(), bind().

let name={
firstName:"shubham",
lastName:"gupta",
printName:function(){
console.log(this.firstName + " " + this.lastName);
}}

let name2={
firstName:"shivam",
lastName:"gupta"
}

name.printName.call(name2);
//output: shivam gupta
Enter fullscreen mode Exit fullscreen mode

That's all about for this post. I hope as a reader of this blog, you get knowledge about this topic after reading this post. Thank you!

Top comments (0)