This question already has answers here:
Is it true that every function in JavaScript is a closure?
(2 answers)
JavaScript closures vs. anonymous functions
(12 answers)
Will a simple function declaration form a closure in JavaScript?
(4 answers)
When closures are created in JavaScript
(4 answers)
Closed 10 months ago.
const a = 'MDNS';
function init() {
console.log(a)
}
init();
Is the above code is a closure?
or Is there any other way we can make closure from single function only.
Related
This question already has answers here:
Explanation of `let` and block scoping with for loops
(5 answers)
Why let and var bindings behave differently using setTimeout function? [duplicate]
(2 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 12 months ago.
In the following code example writes
let does for us is create a unique value of i each time the loop iterates. When our loop is over, we have created six separate values of i that are stored in memory that our console.log(i) statements can access."
How let create a new i in every iteration but only one var i?
This question already has answers here:
JavaScript 'hoisting' [duplicate]
(5 answers)
Javascript function scoping and hoisting
(18 answers)
What are the precise semantics of block-level functions in ES6?
(2 answers)
Closed 1 year ago.
I have come across a very strange JavaScript code. Its execution result is completely different from what I expected. I want to know why.
var a = 1999;
{
console.log(a);
a = 2020;
function a() {}
a = 2021;
}
console.log(a); //Why 2020?
This question already has answers here:
Why can variable declarations always overwrite function declarations?
(3 answers)
javascript hoisting: what would be hoisted first — variable or function?
(2 answers)
Closed 2 years ago.
I expected the answer to be "function text" as output, why the answer is 5?
var alpha = 5;
function alpha(){}
console.log(alpha);
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
What is the 'new' keyword in JavaScript?
(17 answers)
Closed 5 years ago.
I have the following code-
var x=function(){
alert("hello");
}
var z=x;
z();// it works nicely
Now say I modified the code a bit-
var x=function(){
this.show=function(){
alert("hello");
}
}
var z=x;// It doesn't work unless I write var z=new x();
z.show();
I am wondering what difference 'this' keyword is creating between this two function and why I need to use an extra new keyword in the second case.
This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.