Thanks for concepts about reference vs primitives type and heap/stack too! 3:39 string is primitive (copy by value) 6:47 object ,array are references type 7:05 primitives are store on stack 7:35 references are store on heap (take longer to access, store bigger amount of data) 8:40 stack (store like stack 'push on top') 9:16 heap (store pointer) 10:48 it assign a pointer not value 14:50 Object.assign create new object and merge with existing one 16:07 Object.assign doesn't deep clone 17:26 may use Lodash to clone instead 19:04 copy array
Max, I don't know how you make explaining this so simple. If I would try to transfer what I have learned, I would confuse the hell out of the people listening. Thanks a lot for all the content you provide and for making many complicated subjects look actually, again, simple.
The short version, not 20 minute version of this, is that primitives (things that aren't containers for multiple separate values) make copies when passed to a function; while objects (containers for multiple separate values) make *references* to the object when passed to a function. In other words, when something is likely to be relatively small, it's okay to make a copy, but when it can potentially be enormously large, you just say where the thing is. Remember, primitives = copies && objects = references (which means addresses || pointers).
This is excellent, the addition of the stack and heap explanations really clear this topic up. One of those things that is both easier and harder than it looks...
Very good explanation. I remember in C++, for every constructor, you have to have a destructor, otherwise the objects will 'pile up' on the heap = memory leak.
Wow Max, I thought I knew what primitive and reference types were. You simply took these concepts to a whole new level! I didn't know about the stack and the heap at all! Thanks for explaining these amazing concepts.
Brother ! I really respect you. You are the best educator of all time. If you weren't born i couldn't understand these things because there is noone explain these things very specific and easy as you. Thanks
Awesome video, came here because in your Udemy course about Angular you said we should look this subject up (Primitive values vs Reference values). I think I got the main ideas pretty well, I followed your coding in JSbin myself. Thanks Max !
amazing as always. Thank you, Max. Maybe it could be a little bit hard for newbies, but that's the best explanation I saw for about a year of learning JS
Great And this is what I have to say In primitive data types or values, once you get a copy from the declared value its change will not affect the copied variable In reference data type any changes done to the parent variable will alter the variable that's created from it
It was quite long but very good video. You explain everything clearly and slowly with good examples of what you talking about. I didn't know some of these things :)
I have 1.5 years of experience as a software developer. I watched this video about 1.5 years ago. and i said ok i got it. now i'm watching it again and now i'm saying "okay now i get it". I came here from Max's svelte.js course. why do we need to use assignment operation in svelte which does not use push to work with arrays, now everything is very clear. I hope there is no problem in my project where I used the object.assign operation almost on the ground 1 year ago while cloning an object :) Now I understand the value of the deep copy operation. Seeing the questions and answers on stackoverflow I realized that this is a real problem. (My english is not good enough, I hope I was able to express myself. )
Your comment makes perfect sense. Nearly every comment on here is saying how simple this explanation makes it to follow but, honestly, I doubt if even 50% of those people really understand it properly. I've watched it multiple times and there are still a couple of parts that I don't think I'm understanding fully.
Max, I got that Udemy course about nodeJS, I know basics of oop, but came here only to refresh my knowledge, I cant help commenting that you are great lecturer !
Thanks, Buddy . One of the best explanations I have had for primitive and referenced values. The only thing that was missing was the destructuring method (ES6) to clone an object or an array.
Thanks a lot, now I have a clear understanding of what happens with the state in redux, and now I knew that I have a great number of issues with the store of my project, just because I copied the state with spread (...) operator. But I have a state not only with primitive types but with objects and arrays either, and in the future, it may cause problems.
Thank you so much Max for the great explanation I now understand the difference of primitive types and reference types. I really learn a lot in your videos keep it up! 👍
You all time amaze me.You the best I have ever seen.You could also explain Object.freeze() to this lecture.Anyways mutable vs immutable great.I am a big fan of yours.Share your knowledge Sir Max.
When we said var SecondName = name even though the values are the same are the two values located in different places in memory? Are there two different 'Max' in two different memory locations or are they referring to the same 'Max' in the same location in memory?
Thank u for this video. I was wondering before how could i handle this cases in my code. Now its very clear about the object references. You are very helpfull... thank youu..
Great explanation Max. Very succinct. I would add that other array prototype member functions are also immutable (filter, map, reduce). Added you as a recommendation on my channel. Cheers.
Great video diving deeper into reference types and reference values! I was so tired of just hearing people say, "it's a reference type, they aren't the same". Actually seeing how it works in the stack and heap memory space was SO valuable. Question about Object.assign and slice methods for copying objects and arrays: Would an equivalent (and potentially cleaner way of doing it) just be using the spread operator for both? Thanks!
In the example with person and secondPerson, why isn't the same pointer that gets created for person used when we do var secondPerson = person? Is it because if the first variable gets deleted, the first pointer disappears too and we need something "left" to point to the object in memory?
For having written a javascript compiler/interpreter, I think that talking about the stack in javascript is using a low level metaphor that does not have it's place. All the function parameter are evaluated and the results are put in a structure called the closure that will be use to evaluate the function. If function parameters were alway on the stack then when returning a function object, you would lose all the parameter values of the enclosing function. At best, the stack is used as an optimization for this closure if there is no reference of the closure captured in a function instantiation. The location of the content of a closure is implementation specific.
I'm actually taking your ES6 course on Udemy, and I think you should add this to the intro section and it will be more clear. I know you explain the same thing while explaining the ES6 features but that visual ppt really helps me to understand primitive and reference deeply !!! Anyways, 5 stars on the ES6 course and moving on to your Vuejs course !! :)
One silly doubt, when name variable is updated from 'Max' to 'Chris' then on the stack whether the 'Max' is replaced to 'Chris' or a new copy of 'Chris' is made on top of the stack? Thanks.
Excellent video! Very clear and easy to follow with the diagrams. I have only one doubt that can't understand. For example: var obj1 = { p1: "aaa" }; var obj2 = obj1; console.log(obj2.p1); // -> aaa obj1 = null; console.log(obj2.p1); // -> aaa Why obj2 keeps the pointer to object if I set the obj1 to null. Isn't it changing the heap object? I mean, What is the difference of doing it equal to null and setting a property to other value? Thank you!
Thanks for concepts about reference vs primitives type and heap/stack too!
3:39 string is primitive (copy by value)
6:47 object ,array are references type
7:05 primitives are store on stack
7:35 references are store on heap (take longer to access, store bigger amount of data)
8:40 stack (store like stack 'push on top')
9:16 heap (store pointer)
10:48 it assign a pointer not value
14:50 Object.assign create new object and merge with existing one
16:07 Object.assign doesn't deep clone
17:26 may use Lodash to clone instead
19:04 copy array
Yeah man, you did a great job at explaining this stuff, I thought I understood it before but now I really understand it. God bless you 100 man. Peace.
That stack and heap explanation was very insightful. Thank you!
Max, I don't know how you make explaining this so simple. If I would try to transfer what I have learned, I would confuse the hell out of the people listening. Thanks a lot for all the content you provide and for making many complicated subjects look actually, again, simple.
Max, you're very good at explaining complex topics in a simple way that makes them easier to understand. Thanks for the video.
The short version, not 20 minute version of this, is that primitives (things that aren't containers for multiple separate values) make copies when passed to a function; while objects (containers for multiple separate values) make *references* to the object when passed to a function. In other words, when something is likely to be relatively small, it's okay to make a copy, but when it can potentially be enormously large, you just say where the thing is. Remember, primitives = copies && objects = references (which means addresses || pointers).
You can use destructuring to copy object. Although for the array that is stored as a property inside object you will have to use destructuring again.
Brilliant. I've spent the last 3 days trying to grapple with / grasp this, and you pretty much just saved me. Thanks bud!
You deserve award for this explanation .
@@prathameshsawant843 that's called deep cloning
I recommend go through this 20 minutes cause it is worth it!
This is one of the most confusing tutorial i've ever watched.
And I have to watch it again.
And again.
But thank you very much. Well done.
This is excellent, the addition of the stack and heap explanations really clear this topic up. One of those things that is both easier and harder than it looks...
Object.assign({},deepMind.Slice());
Superb in depth explanation You are the best for ever max .
Thanks again
Thanks a million for your fantastic feedback Dheeraj, so cool to read that you like my videos :)
The stack and heap explanation and illustration for these concepts made lightbulbs go off for me. Thank you.
Thank you for this visual explanation, really made me understand. The pictures of the Stack and Heap sure helped as well ! :)
Very good explanation. I remember in C++, for every constructor, you have to have a destructor, otherwise the objects will 'pile up' on the heap = memory leak.
This is the best and most concise explanation I have seen on UA-cam. Thanks
Wow Max, I thought I knew what primitive and reference types were. You simply took these concepts to a whole new level! I didn't know about the stack and the heap at all! Thanks for explaining these amazing concepts.
Thank YOU Juan for such an awesome feedback, really happy to read comments like yours :)
Brother ! I really respect you. You are the best educator of all time. If you weren't born i couldn't understand these things because there is noone explain these things very specific and easy as you. Thanks
Awesome video, came here because in your Udemy course about Angular you said we should look this subject up (Primitive values vs Reference values). I think I got the main ideas pretty well, I followed your coding in JSbin myself. Thanks Max !
Recently started angular. Getting issues just because of object and arrays are getting changed. you saved me. thanks for the great tutorial.
Awesome to hear that, thanks for your great review!
I'm impressed by your fluent and well-prepared explaining. your gestures are also very entertaining haha, thanks for doing what you do.
Nice to hear that, thanks so much ;)
amazing as always. Thank you, Max. Maybe it could be a little bit hard for newbies, but that's the best explanation I saw for about a year of learning JS
Thanks a lot Irop!
Thanks Max, the best explanation I have ever got on this subject.
Really amazing to hear that, thanks so much Rob!
ua-cam.com/video/SnXR6kX1SqA/v-deo.html try this
Great
And this is what I have to say
In primitive data types or values, once you get a copy from the declared value its change will not affect the copied variable
In reference data type any changes done to the parent variable will alter the variable that's created from it
This actually helped me understand how to use pointers and reference values in C++, too!
Thank you Max! :D
Thanks bro it is really helpful. The only thing missing here is possiblity of using JSON to make a deep copy.
A shorthand way to clone the object and its array with the spread operator
const person3 = {
...person2,
hobbies: [
...person2.hobbies
]
}
I think it works if you just use this:
const person3 = {
...person2}
@@yezhang1927 No, with your solution, it not works. Paul's right.
A better way to solve this is
const person3 = JSON.parse(JSON.stringify(person2));
It was quite long but very good video. You explain everything clearly and slowly with good examples of what you talking about. I didn't know some of these things :)
Thank you, happy to read that you liked it :)
I have 1.5 years of experience as a software developer. I watched this video about 1.5 years ago. and i said ok i got it. now i'm watching it again and now i'm saying "okay now i get it". I came here from Max's svelte.js course. why do we need to use assignment operation in svelte which does not use push to work with arrays, now everything is very clear. I hope there is no problem in my project where I used the object.assign operation almost on the ground 1 year ago while cloning an object :) Now I understand the value of the deep copy operation. Seeing the questions and answers on stackoverflow I realized that this is a real problem. (My english is not good enough, I hope I was able to express myself. )
Your comment makes perfect sense. Nearly every comment on here is saying how simple this explanation makes it to follow but, honestly, I doubt if even 50% of those people really understand it properly. I've watched it multiple times and there are still a couple of parts that I don't think I'm understanding fully.
Thanks man, your explanation has saved me from hours of headache.
Really great to read that Hadyan :)
best primitive and reference tutorial forever
Thank you, awesome to read that!
Loved this explenation. My head is indeeds teaming but the core principle is clear. Will rewatch and practice with this.
You did the explanation as best as you could. Thanks a lot!
I came across the same problem, glad you put the video at the right time.
Haha, awesome to hear my timing was right Ganesh :D
Max, I got that Udemy course about nodeJS, I know basics of oop, but came here only to refresh my knowledge, I cant help commenting that you are great lecturer !
Thanks so much for your awesome support here and on Udemy Marcin!
Perfectly and deeply explained! Loved it. Thanks :)
Good that you covered the subject with Stack and heap explanation. Thanks Max :)
Thanks a lot for your great feedback Utsav, really appreciate that :)
Thanks, Buddy . One of the best explanations I have had for primitive and referenced values. The only thing that was missing was the destructuring method (ES6) to clone an object or an array.
You're right, that's a great way of cloning an object or array
JSON.parse(JSON.stringify(object)) beter way to deeply clone objects
That makes sense. thank u.
Earlier I was bit confused why u took long steps to copy in one example of your react course.
Thanks a lot, now I have a clear understanding of what happens with the state in redux, and now I knew that I have a great number of issues with the store of my project, just because I copied the state with spread (...) operator. But I have a state not only with primitive types but with objects and arrays either, and in the future, it may cause problems.
Thank you so much Max for the great explanation I now understand the difference of primitive types and reference types. I really learn a lot in your videos keep it up! 👍
Really happy to hear that this video was helpful! :)
Thank you so much for this video. It was explained so well. I don’t think I’ll ever get confused between the two.
Happy to read that the video was helpful for you Aneesh, thank you :)
Very well explained. Missed your vids Max.
But my videos were never paused? Anyways, happy to hear you liked this one!
But they were paused for me. After watching your React and Redux tuts, thanks to you, I was too busy working on a project :D
it is a one of best explanation sir.you have well planned the example and content.keep doing.
It's just fantastic to read that, thanks a lot!
Great explanation.I think i understood most of what you said 👏
Holy shit max , you are amazing , i was confused about heap and stack for months , you are my man .....
Really great to hear you're liking it - thank you so much! :)
Amazing explanation man ! wow you really have a talent to break things down to understandable bits
THE BEST EXPLANATION EVER MAX !
Thanks so much Aayushi, this really means a lot to me!
The explanation was soo good. Thank you!
Great explanation! Thorough and covered all feasible cases
Thanks a lot for your nice feedback Ben!
Great explanation. Super important concepts.
Great explanations. Keep up the good work, Max!
You all time amaze me.You the best I have ever seen.You could also explain Object.freeze() to this lecture.Anyways mutable vs immutable great.I am a big fan of yours.Share your knowledge Sir Max.
In case anyone is wondering, you can make a deep copy by doing:
let newObj = JSON.parse(JSON.stringify(oldObj))
So helpful. A little thing, yet something that has trouble staying in place in my mind. Thanks.
When we said var SecondName = name even though the values are the same are the two values located in different places in memory? Are there two different 'Max' in two different memory locations or are they referring to the same 'Max' in the same location in memory?
Thank u for this video. I was wondering before how could i handle this cases in my code. Now its very clear about the object references. You are very helpfull... thank youu..
How awesome is the explanation...Yup it was little bit longer but i can asure you of getting it if u watch it for the second time. #thanksMaxy
happy to learn from you mr max thank you for the explanation
Dude you are very useful. Become a teacher cause you clarified it very well.
That was a fantastic explanation and use of example.
Thank you!
Max' content is always great
at 11:34, why didnt the names in the console.log of other two [object object] change to chris??????????????//
that was a pretty nice explaination of a complex concept.
Happy to read that, thank you!
I am completely enjoying Your Nde.js course
Happy to read that Abidul, thank you for your support!
That was so helpful. Great explanation. Thanks so much!
This is an extremely powerfull tut. Thank you.
wow you make this concept very simple. Thanks Max
Happy to hear that this was helpful! :)
awesome explanation!
Really helpful. Thanks for it.
Maxi-baby you explained that REALLY well. Thank you, I totally get it now 👍🏻
I'd pay big money to have JS return "Max" on 6:15 and see Max's reaction.
Haha
Great explanation Max. Very succinct. I would add that other array prototype member functions are also immutable (filter, map, reduce). Added you as a recommendation on my channel. Cheers.
That's true, great addition! I'm happy you're liking it Brian!
Well explanation Mx, you did a great job
Nice to know that, well explained. thanks!
Dankeschön! 🤘🏻
Thank u so much dude u completely cleared my concept
Thank you for this visual explanation, really made me understand.
Awasome. The best explanation!
Thank you so much, I'm glad you liked it!
Max, you are simply awesome. I love you 😘
Great video diving deeper into reference types and reference values! I was so tired of just hearing people say, "it's a reference type, they aren't the same". Actually seeing how it works in the stack and heap memory space was SO valuable.
Question about Object.assign and slice methods for copying objects and arrays: Would an equivalent (and potentially cleaner way of doing it) just be using the spread operator for both?
Thanks!
Thank you so much.
You clear lot of important points for me. 😊👌
ua-cam.com/video/SnXR6kX1SqA/v-deo.html
excellent explanation, thank you Max.
So happy to read that Ravi thank you very much!
U explained it so well❣💯
Thank you so much... Nobody could explain it better than this😊
Thank you very very much for your wonderful feedback Roni, this honestly means a lot to me :)
Hey Max!, really nice udemy courses that you have... greetings from Chile!
Really happy to hear you're liking them Gabriel - thanks for your amazing feedback!
thank you for the video and the simple explanation
Great, Clear my confusion. Thanks a lot
That's really great to read, thanks a lot for your comment!
nice and easy explanation! Thanks!
thank you! cleared things up in my head
In the example with person and secondPerson, why isn't the same pointer that gets created for person used when we do var secondPerson = person?
Is it because if the first variable gets deleted, the first pointer disappears too and we need something "left" to point to the object in memory?
For having written a javascript compiler/interpreter, I think that talking about the stack in javascript is using a low level metaphor that does not have it's place. All the function parameter are evaluated and the results are put in a structure called the closure that will be use to evaluate the function. If function parameters were alway on the stack then when returning a function object, you would lose all the parameter values of the enclosing function. At best, the stack is used as an optimization for this closure if there is no reference of the closure captured in a function instantiation. The location of the content of a closure is implementation specific.
Thanks Max, you explained so well !!!! It helps me a lot !!
Thanks a lot Jack, great to hear that :)
I'm actually taking your ES6 course on Udemy, and I think you should add this to the intro section and it will be more clear. I know you explain the same thing while explaining the ES6 features but that visual ppt really helps me to understand primitive and reference deeply !!! Anyways, 5 stars on the ES6 course and moving on to your Vuejs course !! :)
Thank you very much for this video! :) It was very interesting and I learnt a lot!
Extremely well explained. Thank you.
Really happy to hear that, thanks so much Chris!
Thanks as always Max
Heay Max,
Awesome as usual. Awesome explanation. please please keep it up.
I'll try my best, thanks so much for your awesome feedback!
Pretty clear, Thanks Max!
Very happy to read that Eman, thank you for your comment!
One silly doubt, when name variable is updated from 'Max' to 'Chris' then on the stack whether the 'Max' is replaced to 'Chris' or a new copy of 'Chris' is made on top of the stack? Thanks.
Thank you very much... You are great ✌🏼👍🏼
coming from the react course , very very useful , is there any videos like this topic to understand more everything deep in stack .!
Amazing video, I'll recommend ❤
Excellent video!
Very clear and easy to follow with the diagrams.
I have only one doubt that can't understand.
For example:
var obj1 = { p1: "aaa" };
var obj2 = obj1;
console.log(obj2.p1); // -> aaa
obj1 = null;
console.log(obj2.p1); // -> aaa
Why obj2 keeps the pointer to object if I set the obj1 to null.
Isn't it changing the heap object?
I mean, What is the difference of doing it equal to null and setting a property to other value?
Thank you!