Objects, unlike primitives, are passed by reference. This means that changing an object inside the function will cause it to change outside the function as well. See an example:
function func(arr) {
arr[0] = '!';
}
let arr = [1, 2, 3];
func(arr);
console.log(arr); // shows ['!', 2, 3]
Determine what will be output to the console without running the code:
function func(obj) {
obj.a = '!';
}
let obj = {a: 1, b: 2, c: 3};
func(obj);
console.log(obj);
Determine what will be output to the console without running the code:
function func(arg) {
arg = '!';
}
let obj = {a: 1, b: 2, c: 3};
func(obj.a);
console.log(obj);
Determine what will be output to the console without running the code:
function func(obj) {
obj = '!';
}
let obj = {a: 1, b: 2, c: 3};
func(obj.a);
console.log(obj);
Determine what will be output to the console without running the code:
function func(arr) {
arr.splice(1, 1);
}
let arr = [1, 2, 3];
func(arr);
console.log(arr);
Determine what will be output to the console without running the code:
function func(arr) {
arr.slice(1, 1);
}
let arr = [1, 2, 3];
func(arr);
console.log(arr);
Determine what will be output to the console without running the code:
function func(arr) {
let newArr = arr;
newArr[0] = '!';
}
let arr = [1, 2, 3];
func(arr);
console.log(arr);