Knowing the difference between shallow copies and deep copies of structural data is foundational knowledge for coding pure functions. In this advanced Javascript tutorial, you will learn about primitive vs structural data, passing values vs passing references, mutable vs immutable data, and how to make both shallow and deep copies of structural data. If you are just starting to learn Javascript, I recommend starting with my 8 hour full course tutorial here: kzbin.info/www/bejne/e5eknWyYrN-JkM0
@prudhvichinnam14883 жыл бұрын
Why these guy didn't get views please support him we love davegray
@DaveGrayTeachesCode3 жыл бұрын
Thank you for the support! 🙏💯
@rockfox52 жыл бұрын
Dave, you are an amazing teacher. I have built an entire ePOS system on my own while studying how to program. Your tutorials on Node and express helped me a lot on building my backend server. Your teaching are on point and easy to understand. I’m buying you a coffee. Thank you brother
@DaveGrayTeachesCode2 жыл бұрын
Thank you for the great feedback! I'm glad I have help you, and thank you for the support!
@aileenchan37412 жыл бұрын
Dave, you shared so much important information here! Thank you so much!!! This is essential to pass any technical interview and I'm super thankful for this video!
@DaveGrayTeachesCode2 жыл бұрын
You're very welcome! 💯
@nagaistark2 жыл бұрын
Your videos are the ones that take the longest to understand, but also the ones that are the most informative.
@DaveGrayTeachesCode2 жыл бұрын
I'm glad they help 💯🙏
@moeibrahim65452 жыл бұрын
Man this is one of my favorite js channels
@DaveGrayTeachesCode2 жыл бұрын
Thank you, Moe! 💯🙏
@KentaroxKondo6 ай бұрын
I had been struggling with some problems in my project literally for days, but thanks to this video, I managed to find a solution. Thank you sooo much for setting me free! 😂
@belafekete3125 Жыл бұрын
Great video again. This is the best explanation of shallow vs deep copy in javascript. Thanks for sharing your knowledge!
@devworks8787 Жыл бұрын
For many years I understood shallow and deep copy concept very wrongly. Damn! Thank you for this video!
@letshope5646 Жыл бұрын
Dave, you have an exceptional knowledge in JavaScript. Much appreciated.Thanks
@dardaC1372 жыл бұрын
I have watched a lot tuts on this thing .but the way u explaining every details is boosting my love for JS. Thanks a lot .
@DaveGrayTeachesCode2 жыл бұрын
Glad to hear that! You're welcome! 💯🚀
@judeleon84852 жыл бұрын
Thanks Dave. Your tutorials are excellent. However, I want to suggest that you demarcate your output when you log it out on the console. So, it would be easy for someone to know what section of the code is being displayed at what section on the console. Outputs on the console can be confusing and diifficult to comphrehend without demarcation.
@DaveGrayTeachesCode2 жыл бұрын
You're welcome, Jude 🙏 Good suggestion!
@imakshith2 ай бұрын
very informative David! thanks for sharing the content like this
@niranjanpandit6225 Жыл бұрын
This is the best explaination of shallow vs deep copy in javascript. Keep it up Dave. You are a great teacher👍👍
@DaveGrayTeachesCode Жыл бұрын
Thank you for the kind words!
@matinsasan2 жыл бұрын
You're a great teacher. You know so well how to convey the intended educational message about an advanced topic in accumulating small digestible chunks, helping one to have a solid understanding why things work this way, not just how.
@DaveGrayTeachesCode2 жыл бұрын
Thank you for the kind words 🙏
@tomislavzivkovic3978 Жыл бұрын
Bravo!!! Finally explained understandable and clear.
@DaveGrayTeachesCode Жыл бұрын
Glad it was helpful!
@a_maxed_out_handle_of_30_chars Жыл бұрын
thank you, the video was simple but a little confusing with all the code in single file and the console outputs but very valuable :)
@divyanshupandey82 жыл бұрын
Wonderful explanation Dave!!. Thanks
@DaveGrayTeachesCode2 жыл бұрын
Very welcome!
@anshumanpati54344 ай бұрын
Dave Thanks for making such good tutorials, none of the youtube channel have such precise content... :)
@DaveGrayTeachesCode4 ай бұрын
Glad you like them!
@SalomDunyoIT3 жыл бұрын
Hi. A perfect explanation of fundamentals. I'm curious why Your channel is not that popular. You should collab with some channels like, Traversy Media, Dev Ed, and others so others can learn more. send a request for collab. I think They don't refuse.
@DaveGrayTeachesCode3 жыл бұрын
Thanks! I do have a video on Traversy Media and one on freeCodeCamp. No viral videos yet, but the channel is growing. 🚀 I just started last year, so I'll keep at it. 😃 Thanks for watching and subscribing. 🙏
@vjmathew69622 жыл бұрын
Thanks Dave, very important information i was unware of. Thanks again
@DaveGrayTeachesCode2 жыл бұрын
You're welcome, VJ!
@danilo86petrovic Жыл бұрын
This is so greatly explained... Thank you!
@DaveGrayTeachesCode Жыл бұрын
Glad it was helpful!
@arsyaswanth52 жыл бұрын
Such a Great Explanation. You Deserve more views. Thank you. Got many concepts clarified from this video
@DaveGrayTeachesCode2 жыл бұрын
You're welcome! 💯
@christian-schubert3 жыл бұрын
Thank you very much for sharing this, probably the most comprehensive approach on this topic yet. The deepClone function is a really elegant proposition for nested data types, however, it doesn't seem to solve the problem copying some of the aforementioned complex data types pose (for instance nested Dates in objects).
@DaveGrayTeachesCode3 жыл бұрын
Thanks for watching and for the feedback. I'll have to explore that again in the future! Please share any further findings.
@VanderleiRobertoMoretto2 жыл бұрын
A PRO classe of data structures!
@zal2213 жыл бұрын
Wow I thought I was past this point but I learned aot and I like how you sum things up in the end.
@DaveGrayTeachesCode3 жыл бұрын
Thanks! 🙏🚀
@hosamgnaba32052 жыл бұрын
man i give you a like and subscribed to your channel right away, in my opinion your lesson are valuable, because even advanced programmer still get this kinda stuff wrong , thank you so much
@DaveGrayTeachesCode2 жыл бұрын
You're very welcome! 💯
@mackcoulibaly99812 жыл бұрын
Great stuff. I enjoy all your tutorials. Do you teach in a more formal setting? If so, where? I want to take some online advanced js course with projects. Again, thanks for the excellent material you produce🙏🏼
@DaveGrayTeachesCode2 жыл бұрын
Thank you, Mack! I'm currently a full-time dev, but also teach as an adjunct at FHSU. I was previously a full-time instructor before going back to industry.
@shineLouisShine2 жыл бұрын
Thanks. Another principle to practice independently, and exercises could have been awesome, but... very interesting indeed.
@BURBXN3 жыл бұрын
Great video. Everything was thoroughly explained and covered completely. Thanks for sharing your knowledge!
@DaveGrayTeachesCode3 жыл бұрын
Glad it was helpful! 💯
@Robert-ed5dv3 жыл бұрын
Well explained, just what I was looking for since I work with React and need to prevent the state from mutating when dealing with nested data structures.
@DaveGrayTeachesCode3 жыл бұрын
Great! Glad it was helpful. I'm planning some React tutorials for the summer. 😃
@Robert-ed5dv3 жыл бұрын
Nice! Looking forward to that.
@vishnunair99333 жыл бұрын
Yet another great video, Dave! I hope you get more likes and subscriptions soon. You are one of the most underrated content creators in the tech space.
@DaveGrayTeachesCode3 жыл бұрын
Thank you! 🙏 The channel is growing, and I appreciate the support. 💯🚀
@vishnunair99333 жыл бұрын
@@DaveGrayTeachesCode Your series on JavaScript is amazing! I hope you make a video series on Sveltekit and Svelte someday. I know you are busy with the series on React :).
@גלמלאך2 жыл бұрын
The best video I ever seen!!
@DaveGrayTeachesCode2 жыл бұрын
Thank you!
@gordonmullen72723 жыл бұрын
Thank you so much for explaining the details and can't wait for the follow-up with pue functions. It seems to me that we're going to see a lot about functional programming in the future or either a mixing, if you will, of oop , functional, and imperative to an extent.
@DaveGrayTeachesCode3 жыл бұрын
You're welcome! 🙏💯
@aleksd2862 жыл бұрын
Thanks Dave!
@DaveGrayTeachesCode2 жыл бұрын
You're welcome! 💯
@prudhvichinnam14883 жыл бұрын
Great content
@DaveGrayTeachesCode3 жыл бұрын
Thanks!
@matecode3592 жыл бұрын
Great explanation, thank u :)
@DaveGrayTeachesCode2 жыл бұрын
You are welcome! 💯
@saitejagatadi97112 жыл бұрын
I learnt lot of things in this one video itself
@DaveGrayTeachesCode2 жыл бұрын
Glad to hear that! 🚀
@GeorgiosAmsterdam Жыл бұрын
This is great stuff. Thanks
@DaveGrayTeachesCode Жыл бұрын
You're welcome!
@ganeshanr20282 жыл бұрын
structuredClone() is used to make deep copy easier.
@DaveGrayTeachesCode2 жыл бұрын
Agreed. It wasn't available when I made this video. I could do another on it.
@Shuyinz2 жыл бұрын
This is a great video! Thanks Dave! A question here! When using React, I often use spread operator or concat to make copy from prev state of the object, but it seems like this is pointless to do so after watching your video. This is because with this React practice, it seems we are always doing shallow copy? What is the alternative to write instead if we wants to write a deep copy for setState for example? Correct me if I am mistaking something here! Thanks Dave!
@DaveGrayTeachesCode2 жыл бұрын
Great comment and question, Kim! If you have nested objects, a deep copy is ideal. Previously, we were creating our own deep copy function like in this tutorial or using another library with one like Lodash, but now, JS has recently added support for a native deep copy method called structuredClone. I need to make a tutorial about it: developer.mozilla.org/en-US/docs/Web/API/structuredClone ...Again, it is ok to use the spread operator most of the time, but nested object structures need this.
@Shuyinz2 жыл бұрын
@@DaveGrayTeachesCode wow! Didn't know structuredClone existed! This is very useful! Thank you for reply Dave! Really appreciate for sharing wisdom to us!
@fieldmarshalgaig4856 Жыл бұрын
literally all my alevel coursework problems summed up right here
@azizsobirjonov44622 жыл бұрын
great job but maybe it does not work with Date properties))) you should to add "... || obj instanceof Date" to the condition of the first if statements.
@DaveGrayTeachesCode2 жыл бұрын
Nice note! 🙏 Looking at the new structuredClone now available in JS for a follow up to this. 🚀
@DineshVutukuru2 жыл бұрын
Wow, clean explanation
@DaveGrayTeachesCode2 жыл бұрын
Glad it was helpful!
@DineshVutukuru2 жыл бұрын
@@DaveGrayTeachesCode Indeed it is
@quietcorner5989 Жыл бұрын
Hey dave I have a question in the example of nested structural data type u said that vArray ad yArray have the same reference !! but when I log (vArray === yArray ) to the console I get false which means that they don't point to the same value in Memory !! please clarifiy this to me and also when I do vArray.push(5); instead of vArray[4].push(5); ==> yArray still the same and it didn't change its value!!
@vesnamohar42737 ай бұрын
Thank you!
@DaveGrayTeachesCode7 ай бұрын
You're welcome!
@williamrobson50942 жыл бұрын
Great video! correct me if I'm wrong but, I was under the impression that a for in loop did not always iterate sequentially. Could this cause a problem in the deepClone example function?
@DaveGrayTeachesCode2 жыл бұрын
No problems with an object and for in loops unless the keys are symbols. You may be remembering back to a discussion like this: stackoverflow.com/questions/38086774/does-the-for-in-loop-construct-preserve-order MDN does note that modifying during iteration can cause problems: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in If you have other references on this topic, I'm interested! 💯 In the meantime, you may also be interested in this video on maintaining synchronous iterations during async calls: kzbin.info/www/bejne/ap3Ue3V4etWdiKc
@DrazinChetnik72 жыл бұрын
Maybe you would like to explain obj.structuredClone() to simplify deep copy. :)
@DaveGrayTeachesCode2 жыл бұрын
Maybe so! I'm sure you already know structuredClone is a very newly supported feature. I created this tutorial in the middle of 2021 and according to caniuse.com/?search=structuredClone the earliest support for structuredClone in Chrome was Jan 31st, 2022. It seems like a great JS addition and would make a good follow up to this tutorial: developer.mozilla.org/en-US/docs/Web/API/structuredClone
@talhabinkhalilmuhmmad41592 жыл бұрын
Please do some videos for advanced react concepts and complete react projects
@DaveGrayTeachesCode2 жыл бұрын
Please check the React Tutorials playlist on my channel, and also, I recently completed a MERN Stack Project series.
@ineedsolution2 жыл бұрын
Is it a Gary Holt Signature ESP guitar in the background?
@DaveGrayTeachesCode2 жыл бұрын
No, it has been months, but I think you are seeing my Peavey Wolfgang with custom EVH paint.
@doniaelfouly41425 ай бұрын
Thanks
@ahmad-murery3 жыл бұрын
Very informative and must to know concepts in js, One should have a deepClone function in his utils.js toolbelt, Just a question regarding passing deepClone function as an argument to pureAddToScoreHistory function, can it be considered some sort of Dependency Injection? Thanks Dave
@DaveGrayTeachesCode3 жыл бұрын
I agree about adding this to your utility function module. 💯 And good question! My answer: yes, I agree! I wanted to be explicit with the arguments because a pure function should be. Great to see your comment! 🙏💯
@ahmad-murery3 жыл бұрын
@@DaveGrayTeachesCode I hesitated a little bit before writing my comment but then I said I'll try it this time and see 😎 Thanks, waiting for your next video
@aeronwolfe7072 Жыл бұрын
thank you so much
@DaveGrayTeachesCode Жыл бұрын
You're welcome!
@narenk80772 жыл бұрын
Do you have any plans for bootstrap, scss?
@DaveGrayTeachesCode2 жыл бұрын
I may do Bootstrap in the future. Tailwind is trending. SASS/SCSS is always good. So many topics and not enough time! 💯
@sergiorey20493 жыл бұрын
Amazing
@robx_0012 жыл бұрын
very good
@DaveGrayTeachesCode2 жыл бұрын
Thanks!
@narenk80772 жыл бұрын
How to compare both objects?
@DaveGrayTeachesCode2 жыл бұрын
That's what this tutorial is about. Notice the discussion of referential equality in the beginning of the tutorial.
@FarhadAliev-z1f3 ай бұрын
I wanted have a deep copy with pure function for this object const x = { name : 'Ali', x_funct: ()=>{ console.log('Hello') }, date : new Date() } but date is not visible when I am logging it { name: 'Ali', x_funct: [Function: x_funct], date: 2024-07-27T10:20:23.993Z } { name: 'Farhad', x_funct: [Function: x_funct], date: {} }
Hi Dave, thanks for the amazingly knowledgeable video. I have a question regarding the definition of shallow copy. From my research on geeksforgeeks and MDN docs, the definition of shallow copy differs from your definition and demonstration of shallow copy shown in this video around 8 minutes mark to 10 minutes mark. On MDN doc, it says “A shallow copy of an object is a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made. As a result, when you change either the source or the copy, you may also cause the other object to change too - and so, you may end up unintentionally causing changes to the source or copy that you don't expect. That behavior contrasts with the behavior of a deep copy, in which the source and copy are completely independent.” developer.mozilla.org/en-US/docs/Glossary/Shallow_copy On the other hand, you said in the video around 9:50 mark that we’ve made a shallow copy using the spread operator, although the yArray and zArray are not sharing the same reference, which from my understanding differs from the definition of shallow copy (do share the same reference, thereby copy change causes the source change as well) written on MDN docs and GeeksforGeeks website. Can you clear my head plz. I’m losing it haha. Thank you! Your tutorials are so amazing and I’m so grateful for it :)
@DaveGrayTeachesCode2 жыл бұрын
That's a very thoughtful and well-researched question! Good work! It appears my use of the phrase "shallow copy" should be "shallow-clone" as referenced under MDN's spread syntax: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax - search for "shallow" on the page. The overall concept remains the same. Replace my use of the word copy for the word clone if it creates confusion. 💯🚀
@1000ylovers2 жыл бұрын
@@DaveGrayTeachesCode Thank you for your reply :) So is there a difference between shallow copy and shallow clone? Or is it just worded differently?
@DaveGrayTeachesCode2 жыл бұрын
At this point - after reviewing more of the MDN page - I'd say they are using the terms somewhat interchangeably. They state, "Shallow-cloning (excluding prototype) or merging of objects is now possible using a shorter syntax..." and proceed to show spread syntax examples of making a shallow clone / copy. On the same page they state, "Spread syntax effectively goes one level deep while copying an array. Therefore, it may be unsuitable for copying multidimensional arrays, as the following example shows." - which is what I was showing in this video and how to make a deep copy if needed. Overall, don't let the wording confuse you. As long as you understand spread syntax will not copy / clone nested objects like a deep copy, you've got this.
@1000ylovers2 жыл бұрын
@@DaveGrayTeachesCode Thank you again for the very quick reply. I now understand haha. Yeah that Interchangeably using the copy and clone was throwing me off. You are amazing Dave!😄