DEV Community

Cover image for πŸ”₯ Advanced JavaScript Concepts.😎
Shaik Dawood
Shaik Dawood

Posted on β€’ Edited on

πŸ”₯ Advanced JavaScript Concepts.😎

Hello everyone,πŸ‘‹

If you are new to JavaScript, it can be little bit of challenging to understand some core and Advance topics πŸ€·β€β™‚οΈ. But don't worry keep your fingers crossed🀞 because in this article you'll get full information regarding Advanced JS Topics😁.

⚑ 1. Closures:

πŸ‘‰ MDN Def: A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).

Doesn't sound great to you???πŸ€” Don't worry. Stay tuned.πŸ”₯

So, Basically a function bind together with its lexical environment or lexical scope is known as Closer. Let's see what exactly is lexical scope.
closure1

⚑The word lexical refers to the fact that lexical scoping uses the location where a variable is declared. Nested functions have access to variables declared in their outer scope.

closure2

⚑ Running this code has exactly the same effect as the previous example of the x() function above.
⚑ What's different (and interesting) is that the displayName() inner function is returned from the outer function before being executed. The reason is that functions in JavaScript form closures. A closure is the combination of a function and the lexical environment within which that function was declared.

βœ… Every closure has three scopes:

  • Local Scope (Own scope).
  • Outer Functions Scope.
  • Global Scope.

βœ… Let's say a function can access some variables of it's lexical environment or parent scope, the function actually remembers the reference of variable but it's value.

Let's look at the code:

closure3

Now you might have a bit understanding of Closures right.😎

βœ… let's see some of uses of Closures:

  • Module design pattern.
  • Currying.
  • Functions like once.
  • Memoize.
  • Maintaining state in Async World.
  • setTimeouts.
  • Iterators.
  • and so on.......!.

{ .....
..
... to be continued.
}

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay