JavaScript - Reference vs Primitive Values/ Types

  Рет қаралды 154,395

Academind

Academind

Күн бұрын

Пікірлер: 328
@josiahtobas9172
@josiahtobas9172 4 жыл бұрын
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.
@akshhatsrivastava1972
@akshhatsrivastava1972 4 жыл бұрын
That stack and heap explanation was very insightful. Thank you!
@naythaniel
@naythaniel 7 жыл бұрын
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).
@prathameshsawant843
@prathameshsawant843 6 жыл бұрын
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.
@augiefash2239
@augiefash2239 4 жыл бұрын
Brilliant. I've spent the last 3 days trying to grapple with / grasp this, and you pretty much just saved me. Thanks bud!
@jeewanmaharjan2712
@jeewanmaharjan2712 4 жыл бұрын
You deserve award for this explanation .
@inote4266
@inote4266 4 жыл бұрын
@@prathameshsawant843 that's called deep cloning
@rickvian
@rickvian 4 жыл бұрын
I recommend go through this 20 minutes cause it is worth it!
@88spaces
@88spaces 3 жыл бұрын
Max, you're very good at explaining complex topics in a simple way that makes them easier to understand. Thanks for the video.
@samirergaibi874
@samirergaibi874 6 жыл бұрын
Thank you for this visual explanation, really made me understand. The pictures of the Stack and Heap sure helped as well ! :)
@eruston
@eruston 5 жыл бұрын
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.
@paulparton404
@paulparton404 5 жыл бұрын
A shorthand way to clone the object and its array with the spread operator const person3 = { ...person2, hobbies: [ ...person2.hobbies ] }
@yezhang1927
@yezhang1927 5 жыл бұрын
I think it works if you just use this: const person3 = { ...person2}
@brookspatrader
@brookspatrader 5 жыл бұрын
​@@yezhang1927 No, with your solution, it not works. Paul's right.
@bhupenderKeswani
@bhupenderKeswani 3 жыл бұрын
A better way to solve this is const person3 = JSON.parse(JSON.stringify(person2));
@JuanFVasquez
@JuanFVasquez 7 жыл бұрын
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.
@academind
@academind 7 жыл бұрын
Thank YOU Juan for such an awesome feedback, really happy to read comments like yours :)
@nuttaphonr.6498
@nuttaphonr.6498 3 жыл бұрын
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
@dheerajmantena3307
@dheerajmantena3307 6 жыл бұрын
Object.assign({},deepMind.Slice()); Superb in depth explanation You are the best for ever max . Thanks again
@academind
@academind 6 жыл бұрын
Thanks a million for your fantastic feedback Dheeraj, so cool to read that you like my videos :)
@sydneyhill655
@sydneyhill655 2 жыл бұрын
The stack and heap explanation and illustration for these concepts made lightbulbs go off for me. Thank you.
@cdac1645
@cdac1645 7 ай бұрын
This is the best and most concise explanation I have seen on KZbin. Thanks
@ihortsarenko3119
@ihortsarenko3119 5 жыл бұрын
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
@academind
@academind 5 жыл бұрын
Thanks a lot Irop!
@berkan8518
@berkan8518 4 жыл бұрын
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
@tezdogs
@tezdogs 2 жыл бұрын
This actually helped me understand how to use pointers and reference values in C++, too! Thank you Max! :D
@pushpaaji
@pushpaaji 7 жыл бұрын
Recently started angular. Getting issues just because of object and arrays are getting changed. you saved me. thanks for the great tutorial.
@academind
@academind 7 жыл бұрын
Awesome to hear that, thanks for your great review!
@Concentrum
@Concentrum 7 жыл бұрын
I'm impressed by your fluent and well-prepared explaining. your gestures are also very entertaining haha, thanks for doing what you do.
@academind
@academind 7 жыл бұрын
Nice to hear that, thanks so much ;)
@bronstein007
@bronstein007 Жыл бұрын
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!
@MJ-fu4yc
@MJ-fu4yc 5 жыл бұрын
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 !
@academind
@academind 5 жыл бұрын
Thanks so much for your awesome support here and on Udemy Marcin!
@naqeeburrahman4709
@naqeeburrahman4709 4 жыл бұрын
That makes sense. thank u. Earlier I was bit confused why u took long steps to copy in one example of your react course.
@baharehbadiei7698
@baharehbadiei7698 3 ай бұрын
Perfectly and deeply explained! Loved it. Thanks :)
@sunilpatel6542
@sunilpatel6542 7 жыл бұрын
Somethime JSON.stringfy and JSON.parse also works for cloning object
@dayvie9517
@dayvie9517 7 жыл бұрын
But it is a really roundabout way to do it. You want to copy an object, no to make an object to a string and then get an object from a string which is more demanding.
@yuriiyakhnytsia2292
@yuriiyakhnytsia2292 2 жыл бұрын
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.
@xseeginfo5064
@xseeginfo5064 5 жыл бұрын
best primitive and reference tutorial forever
@academind
@academind 5 жыл бұрын
Thank you, awesome to read that!
@dhanooshbargav7215
@dhanooshbargav7215 3 жыл бұрын
You did the explanation as best as you could. Thanks a lot!
@utsavkumar4381
@utsavkumar4381 6 жыл бұрын
Good that you covered the subject with Stack and heap explanation. Thanks Max :)
@academind
@academind 6 жыл бұрын
Thanks a lot for your great feedback Utsav, really appreciate that :)
@jason-pacini
@jason-pacini 3 жыл бұрын
In case anyone is wondering, you can make a deep copy by doing: let newObj = JSON.parse(JSON.stringify(oldObj))
@gabrielhernandez8457
@gabrielhernandez8457 7 жыл бұрын
Hey Max!, really nice udemy courses that you have... greetings from Chile!
@academind
@academind 7 жыл бұрын
Really happy to hear you're liking them Gabriel - thanks for your amazing feedback!
@dominiquefortin5345
@dominiquefortin5345 5 жыл бұрын
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.
@DespicariPirates
@DespicariPirates Жыл бұрын
JSON.parse(JSON.stringify(object)) beter way to deeply clone objects
@aneshkl
@aneshkl 5 жыл бұрын
Thank you so much for this video. It was explained so well. I don’t think I’ll ever get confused between the two.
@academind
@academind 5 жыл бұрын
Happy to read that the video was helpful for you Aneesh, thank you :)
@msalah5214
@msalah5214 4 жыл бұрын
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..
@OmarOnAWave
@OmarOnAWave 3 жыл бұрын
Amazing explanation man ! wow you really have a talent to break things down to understandable bits
@sourishdutta9600
@sourishdutta9600 6 жыл бұрын
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.
@sashab4432
@sashab4432 2 жыл бұрын
The explanation was soo good. Thank you!
@HaifengZhu-pn3uq
@HaifengZhu-pn3uq 6 ай бұрын
Max' content is always great
@yuvakishoredommaraju1092
@yuvakishoredommaraju1092 5 жыл бұрын
we can use the following method for deepCopy var secondPerson = JSON.parse(JSON.stringify(person)) instead of using Lodash library var refpers = { 'name': 'YUVA', 'age': 25, 'hobbies': ['playing', 'coding'] } console.log(refpers); var deepCopy = JSON.parse(JSON.stringify(refpers)); var shallowCopy = refpers; refpers.name = "yuvakishore"; refpers.hobbies.push('roaming'); console.log(deepCopy, shallowCopy, refpers);
@celestia1
@celestia1 3 жыл бұрын
Dude you are very useful. Become a teacher cause you clarified it very well.
@sergiyrudenko905
@sergiyrudenko905 5 жыл бұрын
You can learn c++ pointers to have better understanding of how it's linked in memory. c++ it's very good language to understand how memory work's. I used a lot this concept in 3d modeling for copying object or to make an instance(linked copy) of object.
@huzeyfetas
@huzeyfetas 2 жыл бұрын
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. )
@jtaylor8606
@jtaylor8606 Жыл бұрын
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.
@aabid3375
@aabid3375 5 жыл бұрын
I am completely enjoying Your Nde.js course
@academind
@academind 5 жыл бұрын
Happy to read that Abidul, thank you for your support!
@muradprohere
@muradprohere Жыл бұрын
happy to learn from you mr max thank you for the explanation
@naeliesuttra
@naeliesuttra 8 ай бұрын
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?
@sandeshjadhav2188
@sandeshjadhav2188 4 жыл бұрын
Thank you for this visual explanation, really made me understand.
@maltegb
@maltegb 3 жыл бұрын
I do not understand why JavaScript doesn't have a built-in deep-copy-function for objects, is it so hard to implement?
@prabhasthakur9776
@prabhasthakur9776 2 жыл бұрын
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.
@TheSkis
@TheSkis 7 жыл бұрын
Great explanation! Thorough and covered all feasible cases
@academind
@academind 7 жыл бұрын
Thanks a lot for your nice feedback Ben!
@pantelisvasileiadis2858
@pantelisvasileiadis2858 4 жыл бұрын
thank you! cleared things up in my head
@GroudFrank
@GroudFrank 2 жыл бұрын
Was this nonesense by design or was this a bug they couldn't squash, so they decided to call it a feature? Thanks for your explanation btw! It helped👍🏿
@miladhp5690
@miladhp5690 Жыл бұрын
you can create a shallow copy without reference only if you use `const copy = {...obj}` instead of `Object.assign({}, obj)` :)
@piters101
@piters101 7 жыл бұрын
If we want to copy deep object (and we don't have any functions in object) we can use easily: var copyObject = JSON.parse(JSON.stringify(obj));
@academind
@academind 7 жыл бұрын
That's indeed a nice trick. Of course even easier with ES6/ES7 and object spread operators. But I might dive into that in the future
@nadiaharrismartinez
@nadiaharrismartinez 2 жыл бұрын
That was so helpful. Great explanation. Thanks so much!
@aabid3375
@aabid3375 5 жыл бұрын
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
@karnpratapsingh8860
@karnpratapsingh8860 2 жыл бұрын
awesome explanation! Really helpful. Thanks for it.
@boristheblad
@boristheblad Жыл бұрын
Nice to know that, well explained. thanks!
@Marcel-dt5du
@Marcel-dt5du 6 жыл бұрын
Thank you! Did I understand you correctly at the end; .slice() also does _not _create a new deep copy of an object in the case of an array of objects?
@fallen_turbo
@fallen_turbo Жыл бұрын
Amazing, also I think we can use JSON functions to create deep clones, JSON.parse(JSON.stringify( object/array name ));
@krunoslavbanovac4356
@krunoslavbanovac4356 7 жыл бұрын
Very well explained. Missed your vids Max.
@academind
@academind 7 жыл бұрын
But my videos were never paused? Anyways, happy to hear you liked this one!
@krunoslavbanovac4356
@krunoslavbanovac4356 7 жыл бұрын
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
@petrockspiracy3120
@petrockspiracy3120 5 жыл бұрын
That was a fantastic explanation and use of example.
@academind
@academind 5 жыл бұрын
Thank you!
@waseemansar4077
@waseemansar4077 7 жыл бұрын
wow you make this concept very simple. Thanks Max
@academind
@academind 7 жыл бұрын
Happy to hear that this was helpful! :)
@BeeshoStudying
@BeeshoStudying Жыл бұрын
thank you for the video and the simple explanation
@rohil3023
@rohil3023 3 жыл бұрын
Great explanation.I think i understood most of what you said 👏
@shibleesaidul4963
@shibleesaidul4963 4 жыл бұрын
In javascript stack tells us where we are in the program & all variables and other objects are stored in heap memory. so whether it is primitive or reference type all stored in heap, isn't it???
@abhishekshimpi214
@abhishekshimpi214 3 жыл бұрын
Thank u so much dude u completely cleared my concept
@michelleBSkiLLz
@michelleBSkiLLz 2 жыл бұрын
nice and easy explanation! Thanks!
@nathnolt
@nathnolt 11 ай бұрын
Nowadays, you would use structuredClone(value) instead of JSON.parse(JSON.stringify(value)) for deep copies.
@codebuilders9081
@codebuilders9081 4 жыл бұрын
that was a pretty nice explaination of a complex concept.
@academind
@academind 4 жыл бұрын
Happy to read that, thank you!
@NICHLE.
@NICHLE. Жыл бұрын
Hi Max, thanks for great tuitorials. I am just wondering why didnt you mention spread operator as option to object.assign method because of its deep-cloning ability
@andrew5407
@andrew5407 4 жыл бұрын
Does the Array pointer live on the stack or in the heap ?
@sachithradilshan9673
@sachithradilshan9673 6 жыл бұрын
it is a one of best explanation sir.you have well planned the example and content.keep doing.
@academind
@academind 6 жыл бұрын
It's just fantastic to read that, thanks a lot!
@sumeetsood232
@sumeetsood232 6 жыл бұрын
Thanks Max. Great Video. Can you please show how object is stored in memory if object has a property that gain is a object or array, like you showed how object is stored in memory.
@degreman
@degreman 2 жыл бұрын
Thats a Good explanation, thanks to you
@jasnanajeem9121
@jasnanajeem9121 4 жыл бұрын
at 11:34, why didnt the names in the console.log of other two [object object] change to chris??????????????//
@silambarasanr.d2604
@silambarasanr.d2604 2 жыл бұрын
What if we use the Spread operator to copy the object to a new object?
@paxdriver
@paxdriver 2 жыл бұрын
Introducing the spread operator lol now I use it for everything
@TechSquidTV
@TechSquidTV 7 жыл бұрын
Amazing video as always. Sharing on Twitter!
@academind
@academind 7 жыл бұрын
Awesome, thanks so much!
@chrsbll
@chrsbll 7 жыл бұрын
Extremely well explained. Thank you.
@academind
@academind 7 жыл бұрын
Really happy to hear that, thanks so much Chris!
@tymothylim6550
@tymothylim6550 3 жыл бұрын
Thank you very much for this video! :) It was very interesting and I learnt a lot!
@nigelhing6605
@nigelhing6605 5 жыл бұрын
Just a silly question, but how do you record yourself to show up on the right hand side of the screen? I've seen this technique a lot and I want to know how to do it too :-)
@academind
@academind 5 жыл бұрын
Not a silly questions Nigel - we are sitting in front of a greenscreen and record ourselves + the screen. This is the hardware: kzbin.info/www/bejne/gqDUp56tqbF1iJo And this is how we edit the videos: kzbin.info/www/bejne/iKK9oqCKirSUi9E
@ХристоСтанков-о6к
@ХристоСтанков-о6к 6 жыл бұрын
Awasome. The best explanation!
@academind
@academind 6 жыл бұрын
Thank you so much, I'm glad you liked it!
@OscarCedano
@OscarCedano Жыл бұрын
Thank u bro! This video really helped me!
@anythgofnthg154
@anythgofnthg154 5 жыл бұрын
if you put name = 'Chris' above var secondName = name; , console.log(secondName) would output Chris. I don't see how there are two names in memory. Also, instead of the last line being console.log(secondName), if he'd have written console.log(name) the output would have been 'Chris' not 'Max'. It seems to me that the variable was reassigned in memory, not stored twice. Am I wrong here? Maybe I'm not understanding him correctly.
@rohancherry
@rohancherry 6 жыл бұрын
excellent explanation, thank you Max.
@academind
@academind 6 жыл бұрын
So happy to read that Ravi thank you very much!
@sandeeprao1651
@sandeeprao1651 7 жыл бұрын
Love the way you explain. Thanks Max. Also, halfway through your Angular4 Udemy course. Loving it.
@academind
@academind 7 жыл бұрын
Really awesome to hear that Sandeep, thanks for your feedback! :)
@aakritigoyal1432
@aakritigoyal1432 3 жыл бұрын
U explained it so well❣💯
@swapnilbhargava2927
@swapnilbhargava2927 4 жыл бұрын
Max, you are simply awesome. I love you 😘
@DharmendraSingh4ever
@DharmendraSingh4ever 3 жыл бұрын
Thank you very much... You are great ✌🏼👍🏼
@harshilparmar9076
@harshilparmar9076 4 жыл бұрын
As always Thank you so much !! Can you make a video on how to understand the code and contribute to js repo on Github
@emanfinding2603
@emanfinding2603 6 жыл бұрын
Pretty clear, Thanks Max!
@academind
@academind 6 жыл бұрын
Very happy to read that Eman, thank you for your comment!
@ronidey380
@ronidey380 6 жыл бұрын
Thank you so much... Nobody could explain it better than this😊
@academind
@academind 6 жыл бұрын
Thank you very very much for your wonderful feedback Roni, this honestly means a lot to me :)
@djelasrca8091
@djelasrca8091 Жыл бұрын
Super useful. Thanks
@adamscantley
@adamscantley 7 жыл бұрын
This is absolutely fantastic. Thank you!
@academind
@academind 7 жыл бұрын
Thanks so much Adam, I'm happy you're liking it!
@pankajsharma9801
@pankajsharma9801 4 жыл бұрын
Thank you so much. You clear lot of important points for me. 😊👌
@fishworld2021
@fishworld2021 4 жыл бұрын
kzbin.info/www/bejne/iZ-7g2mhjZaGp6M
@davida.7586
@davida.7586 6 жыл бұрын
Very good explanation. Thank you!
@academind
@academind 6 жыл бұрын
Great to read that you like it David, thank you!
@sharriceowens913
@sharriceowens913 9 ай бұрын
thank you so much you have a new subscriber
@vs599318026
@vs599318026 7 жыл бұрын
Thanks Max, you explained so well !!!! It helps me a lot !!
@academind
@academind 7 жыл бұрын
Thanks a lot Jack, great to hear that :)
@vs599318026
@vs599318026 7 жыл бұрын
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 !! :)
@gcg8187
@gcg8187 Жыл бұрын
thank you, this was helpful
@poongudivanan
@poongudivanan 5 жыл бұрын
Valuable information... !!!! Thanks great..
@sgt-en4orcer92
@sgt-en4orcer92 5 жыл бұрын
Thanks, great explanation!!
@nathaliebenarroch
@nathaliebenarroch 2 жыл бұрын
very well done! thanks
Reference Vs Value In JavaScript
15:12
Web Dev Simplified
Рет қаралды 183 М.
From Small To Giant Pop Corn #katebrush #funny #shorts
00:17
Kate Brush
Рет қаралды 69 МЛН
LIFEHACK😳 Rate our backpacks 1-10 😜🔥🎒
00:13
Diana Belitskay
Рет қаралды 3,9 МЛН
You might not need useEffect() ...
21:45
Academind
Рет қаралды 161 М.
Understanding JavaScript Object References : A Visual Explanation 🧠
25:15
tapaScript by Tapas Adhikary
Рет қаралды 4,3 М.
Angular 18 is EXACTLY what we needed
9:15
Academind
Рет қаралды 81 М.
JS "this" and Function References - What is it all about?
20:20
Primitives Data Types In Java - All the Primitives And What They Do
10:24
Understanding Immutability in JavaScript
9:41
Deborah Kurata
Рет қаралды 5 М.
JavaScript Data Structures: Getting Started
1:36:47
Academind
Рет қаралды 241 М.
Variables and Primitive Types - Javascript In Depth
25:28
Tech with Nader
Рет қаралды 4,2 М.
The Weird History of JavaScript
12:09
Fireship
Рет қаралды 1,2 МЛН