JavaScript stores Reference Values in Heap memory and thus variables stores to a pointer/address. So, if we change original variable, it changes the assigned one as well:
var cols = ['red', 'blue', 'green']
var newCols = cols
cols.push('orange')
console.log(cols)
// returns ["red", "blue", "green", "orange"]
console.log(newCols)
// returns ["red", "blue", "green", "orange"]
However, when I experimenting for reference types, I find out that, when I am using indexing for assignment and push method they act in not same logic which I did not expect:
var cols = ['red', 'blue', 'green']
var newCols=cols[1]
cols[1] = "pink"
console.log(cols)
// returns > ["red", "pink", "green"]
console.log(newCols)
// returns > "blue" WHY?
But:
var cols = ['red', 'blue', 'green']
var newCols=cols
newCols[3] = 'orange'
cols.push('yellow')
console.log(cols)
// returns > ["red", "blue", "green", "orange", "yellow"]
console.log(newCols)
// returns > ["red", "blue", "green", "orange", "yellow"] WHY?
So, what is the logic ('the idea under the hood') here that, in one sample, it affects original, but in another it does not?