Skip to content

Commit b05439a

Browse files
authored
Update js codes.docx
1 parent b0f97c1 commit b05439a

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎js codes.docx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ For JavaScript Theorytical questions: "https://github.com/sudheerj/javascript-in
22
For Reactjs theory questions: https://github.com/sudheerj/reactjs-interview-questions/tree/master/.github/workflows
33
For Google, Facebook, Microsoft coding challenges: https://youtube.com/c/KevinNaughtonJr
44
Jest: https://github.com/sapegin/jest-cheat-sheet
5+
6+
https://learnersbucket.com/javascript-sde-cheat-sheet/
7+
58
================================================================================================================================================================================
69
Code 1: Remove Duplicate characters from String
710
function removeDuplicateCharacters() {
@@ -3008,9 +3011,36 @@ checkSort(-44);
30083011
"priya".split('')
30093012
// ['p', 'r', 'i', 'y', 'a']
30103013
===================================================================================================================================================================
3014+
Deep Copy and Shallow Copy:
3015+
A deep copy means that "all of the values of the new variable" are copied and disconnected from the original variable. It makes a copy of all the members of X, allocates different memory location for Y and then assigns the copied members to Y to achieve deep copy. In this way, if X vanishes Y is still valid in the memory.
3016+
A shallow copy means that certain (sub-)values are still connected to the original variable. It is primarily utilized for copying One Dimensional array elements, where it only copies the elements present at the first level.
3017+
3018+
A deep copying means that value of the new variable is disconnected from the original variable
3019+
while a shallow copy means that some values are still connected to the original variable.
3020+
3021+
addresses of X and Y will be the same i.e. they will be pointing to the same memory location.
30113022

3023+
To copy an object in JavaScript, you have three options. Both spread (...) and Object.assign() perform a shallow copy while the JSON methods carry a deep copy.
3024+
--->Assignment operator “=”
3025+
--->Use the spread (...) syntax
3026+
--->Use the Object.assign() method
3027+
--->Use the JSON.stringify() and JSON.parse() methods --where the stringify() method converts a particular JavaScript object to a string, and then the parse() method performs the parsing operation and returns an object.
30123028

3029+
For a primitive value, you just simply use a simple assignment:The important takeaway here is that you can quickly copy a primitive data type’s exact value
3030+
in a separate memory space by creating and assigning another variable to the variable being copied. Take note of how it is instantiated — const will not
3031+
allow later changes.
30133032

3033+
let counter = 1;
3034+
let copiedCounter = counter;
3035+
copiedCounter = 2; //And when you change the value of the copied variable, the value of the original remains the same.
3036+
console.log(counter);
3037+
Output: 1
3038+
3039+
3040+
However, if you use the assignment operator for a reference value, it will not copy the value. Instead, both variables will reference the same object in the memory:
3041+
3042+
JS objects (as non-primitive data types) differ because they have reference values and those values are mutable. This means they share a memory address when shallow copied.
3043+
30143044

30153045

30163046

0 commit comments

Comments
 (0)