forked from HackYourFuture/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathstep2-7.js
More file actions
27 lines (20 loc) · 766 Bytes
/
step2-7.js
File metadata and controls
27 lines (20 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'use strict';
const x = 9;
function f1(val) {
val = val + 1;
return val;
}
f1(x);
console.log(x);
const y = { x: 9 };
function f2(val) {
val.x = val.x + 1;
return val;
}
f2(y);
console.log(y);
/* In the x-code, we are logging the value of x, which has not changed. If we want this code to return 10,
we need to console.log(f1(x)). In the y-code, y is an object. The properties of an object can be altered this way,
so the function changes the value of x to 10. No alteration has been done to the variable reference itself. If f2
attempted to change the *value* of y (rather than an attribute of its object), it would yield results like the x-code.
For example, if the function is altered, val = { x: 10 }; return val;, it will return { x: 9 } */