Javascript (function (){})() closure [duplicate] - javascript

This question already has answers here:
Javascript How to define multiple variables on a single line?
(10 answers)
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Closed 2 months ago.
Can any one explain why this happens?
// Example 1
(function () {
var a = b = 5;
})();
console.log(b); // Output 5
var a = b = 5 and var b = 5 is totally different?

Related

Javascript Need a basic syntax explaination [duplicate]

This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 2 years ago.
var x = 5;
function test2(x) {
x = 7;
}
test2(8);
alert(x);
Why exactly is this putting out the global var x=5, without being affected by anything within the function.
Because you passed a param called x, which is the same name as your global var. Try this out:
var x = 5;
function test2(y) {
x = y;
}
test2(8);
alert(x);

Function is not changing outer variable [duplicate]

This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 2 years ago.
I'm new to Javascript
The function is not changing the outer variable
function edit(array, letter){
array = 0;
letter = 'b';
}
let string = 'a';
let array = [1,0,2];
edit(array, string);
console.log(array, string);// Result [1,0,2] not 0 b

difficulty in understanding closures [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 4 years ago.
Why this code returns 43 as the result, I expect it to result 42. The code is as follows:
function say667() {
// Local variable that ends up within closure
var num = 42;
var say = function() { console.log(num); }
num++;
return say;
}
var sayNumber = say667();
sayNumber();
You've closed over the variable num, not the value the variable has at the time you define the function.
This is the order of events:
You assign 42 to num
You increment num to 43
You return a function and store it in sayNumber
You call that function, which reads the value of num, which is 43

Is "var foo = []" the same as "var foo = new Array()"? [duplicate]

This question already has answers here:
What's wrong with var x = new Array();
(5 answers)
What is the difference between condensed arrays and literal arrays?
(3 answers)
Closed 6 years ago.
Is typing "var foo = []" the same as typing "var foo = new Array()"?

Store value in the given variable through functions [duplicate]

This question already has answers here:
Pass variables by reference in JavaScript
(16 answers)
Pointers in JavaScript?
(15 answers)
Closed 7 years ago.
Is there any how javascript or jquery to store values in the given variables while interacting through functions wuthout using global vars?
I made an example: http://jsfiddle.net/1fw2urks/3/
$(document).ready(function(){
start();
});
function start(){
var n = 0;
for (i = 0; i < 10; i++){
count(n);
}
alert(n);
}
function count(n){
countAgain(n);
}
function countAgain(n){
n += 1;
}
n var should come with value 10 if it worked, but it seems that we can't change the passed parameters value while in the function.
Is this affirmation right?
(I did some research and i found this answer, but, I am stubborn)

Categories