Search This Blog

2026/08/21

Javascript:Array Slice

 



slice do not alter original array,array's some part is copied to destination copying is shallow
let languages = ["java","javascript","Go",{node:"javascript"}]
let firstLanguage = languages.slice(3,4)

firstLanguage[0]["node"] = "python" //this value being object is shallow copied hence changes witll in parent also

console.log(languages)
console.log(firstLanguage)


Output:
[ 'java', 'javascript', 'Go', { node: 'python' } ]
[ { node: 'python' } ]


primitive types are copies by value

No comments:

Post a Comment