Mutability And Reference VS Privative Types in JavaScript
Mutability And Reference VS Privative Types in JavaScript
Mutability && Primitive && Reference Examples

In JavaScript,
String
values
are immutable, which means that they cannot be altered once
created.
For example, the following code:
var myStr = "Bob";
myStr[0] = "J";
cannot change the value of
myStr
to
Job
, because
the contents of
myStr
cannot be
altered. Note that this does
not mean that
myStr
cannot be
changed, just that the individual characters of a string literal
cannot be changed. The only way to change
myStr
would be
to assign it with a new string, like this:
var myStr = "Bob";
myStr = "Job";
Objects are passed by reference, are mutable, and can be modified by our functions:
function rotateLeft(arr, num) {
for (let i = 0; i < num; i++) {
let el = arr.pop();
arr.unshift(el);
}
}
let myArr = [1, 2, 3, 4, 5, ];
rotateLeft(myArr, 2);
console.log(myArr);
Strings are passed by value, are immutable, and a new array is constructed and returned, because it cannot be changed in place.
function rotateString(str, num) {
return str.slice(num) + str.slice(0, num);
}
let str = "foobar";
let ret = rotateString(str, 3);
console.log(str);
console.log(ret);
Dereferencing
Arrays
To dereference an array, use
let [var1, var2]
syntax.
let arr = ['one', 'two', 'three'];
let [first] = arr;
console.log(first);
Objects
To dereference attributes from an object, use
let {}
syntax.
let me = { | |
name: "Ian", | |
instruments: ['bass', 'synth', 'guitar'], | |
siblings: { | |
brothers: ['Alistair'], | |
sisters: ['Meghan'] | |
} | |
} | |
let { | |
name, | |
instruments: musical_instruments, | |
siblings: { | |
sisters | |
} | |
} = me; | |
console.log(sisters); | |
function printInstruments({ | |
instruments | |
}) { | |
console.log(instruments); | |
} | |
printInstruments(me); | |
function printSiblings({ | |
siblings: { | |
sisters, | |
brothers | |
} | |
}) { | |
console.log("Sisters", sisters); | |
console.log("Brothers", brothers); | |
} | |
printSiblings(me); | |
//rest parameters | |
// combines parameters into array | |
...parameterName | |
splice is an example of where we 've seen this before | |
let arr = 'my dog has fleas'.split(' '); | |
arr.splice(3, 1, 'trees') | |
//spread operator | |
// take an arrray and spread them into arguments | |
...argContainer | |
OR | |
// take an object or array and spread their elements/attributes into another object or array | |
function multiply(multiplier, ...theArgs) { | |
return theArgs.map(function(element) { | |
return multiplier * element | |
}) | |
} | |
let arr = multiply(2, 1, 2, 3) | |
console.log(arr) | |
let me = { | |
name: "Ian", | |
instruments: ['bass', 'synth', 'guitar'], | |
siblings: { | |
brothers: ['Alistair'], | |
sisters: ['Meghan'] | |
} | |
} | |
let countryArr = ['USA', 'Canada', 'UK']; | |
//me[countries] = countryArr; | |
let myCountries = { | |
'countries': countryArr | |
}; | |
let newMe = { | |
...me, | |
...countries | |
} |
// Immutable vs Mutable | |
// Strings are IMMUTABLE | |
let str = "abc"; | |
str[ 1 ] = "x"; | |
// console.log(str); // => 'abc' | |
// We can reassign a variable to another string | |
beforeStr = str; | |
str += "def"; // str = str + 'def' | |
// console.log(str) // 'abcdef' | |
// console.log(str === beforeStr); // false | |
const anotherStr = "abcdef"; | |
// console.log(str === anotherStr); // true | |
// Arrays are MUTABLE | |
let arr = [ "a", "b", "c" ]; | |
let beforeArr = arr; | |
arr[ 1 ] = "x"; | |
console.log( arr ); // ['a', 'x', 'c'] | |
console.log( beforeArr ); // ['a', 'x', 'c'] | |
console.log( arr === beforeArr ); // true | |
let anotherArr = [ "a", "b", "c" ]; // same contents, but different array | |
console.log( arr === anotherArr ); // false | |
let dupArr = arr.slice(); // duplicate of the array | |
console.log( dupArr ); // ['a', 'b', c'] | |
console.log( arr === dupArr ); // false | |
let str = "abc"; | |
let upperCase = str.toUpperCase(); | |
console.log( str ); | |
console.log( upperCase ); | |
console.log( str === upperCase ); | |
console.log( "abc".toUpperCase() ); |
Primitive Data Types in Depth
