The JavaScript Programming Language
Primitive vs Reference Type
1. It is not possible to modify value of primitve data type
var s = "zdravo";
s.toUpperCase(); // returns ZDRAVO
console.log(s); //returns 'zdravo'
//Conclusion: Method doesn't change string value.
2. Primitive types are changed by value, and object properties are changed by references.
//Primitive values are not reference types
var x = 5;
var y = x;
y = 8; //only y is modified. x is not modified
console.log(x); //5
//Objects are reference types
var obj_x = {prop: 13};
var obj_y = obj_x;
obj_y.prop = 21; //obj_x.prop is also modified to 21
console.log(obj_x.prop); //21
3. Equality
a) Two primitive types are equal if they have same value
b) Two arrays are equal if they have same length and elements have same value
function equalArrays(a,b) {
if (a.length != b.length) return false;
for(var i = 0; i < a.length; i++)
if (a[i] !== b[i]) return false;
return true;
}