Are you learning JavaScript these days? Then you must know about the all new and advance concepts of this programming language. There are many new concepts in JS but in this blog we will discuss about the closure in JavaScript.
The demand of JavaScript is increasing day by day in the industry. By learning JS there are many career options are available for you such a web developer, JS developer, Front-end developer and many more.
If you are wondering about web developer, then you can be that by completing web development courses in Delhi.
What is JavaScript?
JavaScript is the most famous programming language. Developers use it for adding interactivity into the static web pages. I short we can say that it gives life to a static page. JavaScript is open source and lightweight.
After making a static website by using HTML and CSS we need JavaScript. If you also want to learn HTML and CSS with experts then must go for HTML5 and CSS3 training institute in Delhi
As discussed above there are many advance concepts in JavaScript. You can learn those topics by pursuing web design courses in Delhi.
Closures are one of them it is very useful and important to learn.
So let’s understand closures in JavaScript.
Closures in JavaScript
Closure is a nested function or a private function can use its parent’s functions properties outside the function.
Let’s see example for more clarification:
function Name(name) {
var a = “Hey, " + name + "!";
var b= function() {
var c = a+ “Good Morning!”;
console.log(a);
};
return b;
}
var b= Name(“ABC”);
There is a function Name and it has a variable (a) after that there is one more function that is function (b). It is called nested function also. The function (b) is closure in this example.
key points you should know about :
Closure can be used only in case of nested functions.
Outer function cannot access the inner scope.
Variable without keyword like var, let, const are always treated as a global even if they are nested.
There are two scopes to every closure.
As described above a nested function is a function inside the other function. So, there are two types of scope i.e. local scope, global scope.
Local Scope: A local scope executes when a variable can only be used inside the function where it is defined. In other words It is only accessible by the own function. Any outer function can use it.
Let’s see an example:
function number() {
Num = 4;
}
Global Scope: A Global scope i s just opposite to the local scope. A global scope execute when a variable can be used by the all function and its can be accessible by window and the whole page.
Let’s see an example:
var Num = 4;
function number() {
return Num * 2;
}
If you want to learn more about JavaScript you join JavaScript course in Delhi.
Comentarios