empty an array passed to a function [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I am trying to empty an array passed to a function within javascript:
var a = [1,2,3];
function emptyArrayNo(ar) {
// the following does not empty the global array
ar = [];
}
function emptyArrayYes(ar) {
// any of the following 3 methods empties the global array
ar.length = 0;
//ar.splice(0, ar.length)
//while(ar.length > 0) {ar.pop();}
}
emptyArrayNo(a);
console.log(a); // returns [1,2,3]
emptyArrayYes(a);
console.log(a); // returns []
All the three methods within emptyArrayYes() seem to work, which I think is due to the reference to the array passed not being modified within the function. However when we set the array to [] within the function, I believe the reference changes therefore causing issues i.e. not being seen at the global scope. Can someone please clarify what is going on here? Thanks.

Related

setInterval and how it interacts with objects [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
JavaScript setInterval and `this` solution
(9 answers)
How do JavaScript closures work?
(86 answers)
Closed 10 months ago.
I've noticed when using setInterval() with objects, it accepts this:
setInterval(function(){auto1.increaseValue(s);}, auto1.interval);
Where auto1 is an object from a class.
However, the program throws an error when I put an object or object of an array of another object inside the function.
Ex.
let index = this.arrayForItems.length-1;
setInterval(function(){this.arrayForItems[index].increaseValue(s);header.innerHTML = s.value;}, this.arrayForItems[index].interval);
setInterval(function(){this.item.increaseValue(s);header.innerHTML = s.value;}, this.item.interval);//Where item is the object inside object shop
But this works.
let index = this.arrayForItems.length-1;
let referenceToPurchase = this.arrayForItems[index];
setInterval(function(){referenceToPurchase.increaseValue(s);header.innerHTML = s.value;}, referenceToPurchase.interval);
Why is this so?

Why the output is different for both the loops? [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 3 years ago.
I expected same output from the two loops
var arr = [5,6,7,8]
// first loop
for(var i=0;i<arr.length;i++) {
setTimeout(()=>{console.log(i,arr[i])},500)
}
// second loop
for(let i=0;i<arr.length;i++) {
setTimeout(()=>{console.log(i,arr[i])},500)
}
does let and var can change the closure property of any function especially in this case?
This is because of the lexical scope.
let will keep the variable value but var will update the value even before the first setTimeout callback will get executed.

How to change the value of a passed variable in a function in javascript? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
Inside a function, I'd like to change the value of a global variable that is specified by the function's argument.
A = 1;
B = 2;
C = 3;
function myFunction(variable) {
variable += 10;
}
myFunction(A);
myFunction(B);
myFunction(C);
I'm looking for these results:
console.log(A); // 11 expected
console.log(B); // 12 expected
console.log(C); // 13 expected
I can't output the new value via a return statement because it's computed inside the callback function of a post request.
How to achieve this?
It's not possible. This is called shadowing, that's that your argument is other variable than global, so A in myFunction is other that A in global
Maybe trying to run with apply() and using this.variable inside function would help

Why do this returns a non expected result? [duplicate]

This question already has answers here:
Why is [1,2] + [3,4] = "1,23,4" in JavaScript?
(14 answers)
Closed 4 years ago.
So I'm learning about the reduce method and I write this code...
function getSum(x,y){
return x+y
}
var arraySum = function(array) {
return array.reduce(getSum)
};
arraySum([1,[2,3],[[4]],5])
But it actually returns a string of the elements all-together...
I'm actually trying to sum them all... I expected the result to be 15 instead of "12,345"
What's happening? what am I doing wrong?
Your problem is you're adding actual arrays together, not the content within the arrays. Add a counter, and an incrementational variable like i inside the variable arraySum, and call the position of i, incrementing it every time, it should fix your problem.

how to pass by reference in javascript [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 8 years ago.
How should I pass by reference in a JavaScript function?
For example:
function passByReference(a){
a = "banana";
}
var x = "apple";
passByReference(x);
Here x should output banana.
I am new to JavaScript; any help would be appreciated. Thanks in advance.
Wrap the variable with an object. Properties of objects are passed by reference.
function passByReference(a) {
a.fruit = 'banana';
}
var wrapper = {fruit: 'apple'};
passByReference(wrapper);
You cannot pass by reference. If you want to modify the state of your caller from within the called function, there are two ways you could do:
Sometimes it fits best to wrap the state in an object:
var x = { valueToChange: 'initialValue' };
passByReference(x);
This works because with objects, a pointer to the address where the object lies is passed. That pointer is passed by value but it still points to the same object.
In some other cases a callback does the trick:
var x = "apple";
passByReference(function (newValue) { x = newValue; });
function passByReference(callback) {
callback("banana");
}
This works because if you define a function as above it builds a closure together with all variables it references. When your function gets called it really modifies the value of x.

Categories