Assigning value to an array or object inside a function [duplicate] - javascript

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 1 year ago.
I am learning javascript and I read that in javascript array and objects are by default passed as a reference. So when I do this:-
var a = [2, 3, 4]
function pushEl(a, num) {
a.push(num)
}
pushEl(a, 5)
console.log(a)
The output is as expected which is
[2,3,4,5]
But what I am not able to understand is that when I assign value to an array or an object inside the function the original array/object is not changed.
var a = [2, 1, 3]
function change(a) {
a = [1, 2]
}
change(a)
console.log(a)
I expect the output to be [1,2] but the output is [2,1,3].
If the array is passed by reference then the changes should have been reflected in the original array too.
Can anybody tell me what concept I'm missing here?

Related

What does this declaration mean in javascript [duplicate]

This question already has answers here:
Syntax - what does square brackets around a variable declaration mean [duplicate]
(1 answer)
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Javascript. Assign array values to multiple variables? [duplicate]
(2 answers)
Closed 8 months ago.
Consider the declarations of variable "x" and "y"
const x = 1;
const [y] = [1]
What is the meaning of 2nd declaration?
Can someone suggest an article about such declaration type.
This is destructuring assignment.
You are unpacking values from arrays or object properties.
In your above code, a const variable y will be defined with value 1.

why my global array has been updated after a function but my other global variable doesn't? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
JS function not updating an object
(5 answers)
Closed 1 year ago.
Imagine two situations below: We have an array which is a global variable and it will be updated after a function. On the other hand as you can see in commented code my other global variable doesn't updated after a function?
I learnt about pass by reference and value, but i am wondering how come array has been passes by reference to the function but "a" variable has been passes as value?
var a=[1,2,3,4,5];
function adder(arrey, item) {
arrey.push(item);
return arrey.shift();
}
console.log(a);
console.log(adder(a,6));
console.log(a);
var a =10
function adder (num) {
num+=1;
return num;
}
console.log(a);
adder(a);
console.log(a)

Syntax - what does square brackets around a variable declaration mean [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

What is this syntax in JavaScript [duplicate]

This question already has answers here:
Multiple assignment in JavaScript? What does `[ a, b, c ] = [ 1, 2, 3 ]` mean?
(4 answers)
Closed 5 years ago.
Take the following line of code
const [component] = router.getMatchedComponents({ ...to })
Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer
It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.
So here in your code:
const [component] = router.getMatchedComponents({ ...to })
You are assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

Variable passed into function only changed within function [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 6 years ago.
I created a simple function and I noticed something unexpected about javascript. When I pass a variable in to a function, then change it, its not changing outside that function. Heres some code:
function check(val, isEven) {
if (val % 2 === 0){
isEven++;
console.log('incremented isEven is ', isEven);
}
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var isEven = 0;
for (var x = 0; x < arr.length; x++) {
check(arr[x], isEven);
console.log('isEven is now ', isEven);
}
Fiddle is here.
Maybe I've been misinterpreting Javascript all these years, but I would have expected the isEven within check() to be the same as the original isEven.....But you can see in the logs, outside isEven stays as 0....
In JavaScript, objects are passed by copying the reference to the object. Primitive types (string/number/etc) are passed by value.
This means if you passed in an object, modifying the object within the function would be reflected outside the function as inside and outside would both have references to the same object.
With a primitive type, the variable is copied when it is passed in and changes inside the function will no longer be reflected in the variable outside the function.

Categories

Resources