JavaScript - Reference vs Primitive Values/ Types

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 เม.ย. 2017
  • One of the most common error sources: Not understanding what the difference between reference and primitive values is. This video will clear up the confusion.
    Need more JavaScript Training?
    Consider my "JavaScript - The Complete Guide" course: acad.link/js
    Stack vs Heap: www.quora.com/What-is-the-dif...
    Object.assign(): developer.mozilla.org/en-US/d...
    Slice(): developer.mozilla.org/en/docs...
    Lodash Clone Deep: lodash.com/docs/#cloneDeep
    Want to become a frontend developer? Consider diving into some of my courses:
    Angular vs React vs Vue - Quickstart and Comparison: acad.link/ng-vs-react-vs-vue
    Ionic + Angular - The Practical Guide: acad.link/ionic
    Angular - The Complete Guide: acad.link/angular
    Vue.js - The Complete Guide: acad.link/vuejs
    You can follow me on Twitter (@maxedapps), Facebook ( / academindchannel ) or visit our Website (www.academind.com).
    See you in the videos!

ความคิดเห็น • 327

  • @nuttaphonr.6498
    @nuttaphonr.6498 3 ปีที่แล้ว +33

    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

  • @josiahtobas9172
    @josiahtobas9172 4 ปีที่แล้ว +63

    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.

  • @naythaniel
    @naythaniel 7 ปีที่แล้ว +103

    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 5 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@prathameshsawant843 that's called deep cloning

    • @rickvian
      @rickvian 3 ปีที่แล้ว +8

      I recommend go through this 20 minutes cause it is worth it!

  • @akshhatsrivastava1972
    @akshhatsrivastava1972 4 ปีที่แล้ว +31

    That stack and heap explanation was very insightful. Thank you!

  • @88spaces
    @88spaces 3 ปีที่แล้ว +8

    Max, you're very good at explaining complex topics in a simple way that makes them easier to understand. Thanks for the video.

  • @energan2184
    @energan2184 3 ปีที่แล้ว +2

    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.

  • @samirergaibi874
    @samirergaibi874 5 ปีที่แล้ว +14

    Thank you for this visual explanation, really made me understand. The pictures of the Stack and Heap sure helped as well ! :)

  • @73dines
    @73dines 4 ปีที่แล้ว +3

    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.

  • @eruston
    @eruston 5 ปีที่แล้ว +3

    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.

  • @jimmysignature915
    @jimmysignature915 2 ปีที่แล้ว +1

    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...

  • @sydneyhill655
    @sydneyhill655 2 ปีที่แล้ว

    The stack and heap explanation and illustration for these concepts made lightbulbs go off for me. Thank you.

  • @RaterisimoCBA
    @RaterisimoCBA 3 ปีที่แล้ว +1

    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 !

  • @JuanFVasquez
    @JuanFVasquez 6 ปีที่แล้ว +3

    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  6 ปีที่แล้ว +1

      Thank YOU Juan for such an awesome feedback, really happy to read comments like yours :)

  • @cdac1645
    @cdac1645 4 หลายเดือนก่อน

    This is the best and most concise explanation I have seen on TH-cam. Thanks

  • @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

  • @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.

  • @tezdogs
    @tezdogs 2 ปีที่แล้ว +1

    This actually helped me understand how to use pointers and reference values in C++, too!
    Thank you Max! :D

  • @ihortsarenko3119
    @ihortsarenko3119 5 ปีที่แล้ว +2

    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!

  • @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 ;)

  • @dhanooshbargav7215
    @dhanooshbargav7215 3 ปีที่แล้ว

    You did the explanation as best as you could. Thanks a lot!

  • @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.

  • @dukewellington5222
    @dukewellington5222 4 ปีที่แล้ว

    Great explanations. Keep up the good work, Max!

  • @53strat55
    @53strat55 2 ปีที่แล้ว

    Loved this explenation. My head is indeeds teaming but the core principle is clear. Will rewatch and practice with this.

  • @OmarOnAWave
    @OmarOnAWave 2 ปีที่แล้ว

    Amazing explanation man ! wow you really have a talent to break things down to understandable bits

  • @scottsaccenti
    @scottsaccenti 4 ปีที่แล้ว

    So helpful. A little thing, yet something that has trouble staying in place in my mind. Thanks.

  • @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 :)

  • @huzeyfetas
    @huzeyfetas 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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.

  • @gabrieljoshuapaet2572
    @gabrieljoshuapaet2572 7 ปีที่แล้ว +1

    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! 👍

    • @academind
      @academind  7 ปีที่แล้ว

      Really happy to hear that this video was helpful! :)

  • @sashab4432
    @sashab4432 2 ปีที่แล้ว

    The explanation was soo good. Thank you!

  • @sandeshjadhav2188
    @sandeshjadhav2188 4 ปีที่แล้ว

    Thank you for this visual explanation, really made me understand.

  • @GaneshKumarButcha
    @GaneshKumarButcha 7 ปีที่แล้ว

    I came across the same problem, glad you put the video at the right time.

    • @academind
      @academind  7 ปีที่แล้ว

      Haha, awesome to hear my timing was right Ganesh :D

  • @baharehbadiei7698
    @baharehbadiei7698 21 วันที่ผ่านมา

    Perfectly and deeply explained! Loved it. Thanks :)

  • @paulparton404
    @paulparton404 5 ปีที่แล้ว +17

    A shorthand way to clone the object and its array with the spread operator
    const person3 = {
    ...person2,
    hobbies: [
    ...person2.hobbies
    ]
    }

    • @yezhang1927
      @yezhang1927 5 ปีที่แล้ว +1

      I think it works if you just use this:
      const person3 = {
      ...person2}

    • @brookspatrader
      @brookspatrader 4 ปีที่แล้ว +1

      ​@@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));

  • @pantelisvasileiadis2858
    @pantelisvasileiadis2858 4 ปีที่แล้ว +1

    thank you! cleared things up in my head

  • @leemacpherson
    @leemacpherson ปีที่แล้ว

    Great explanation. Super important concepts.

  • @saltech2024
    @saltech2024 ปีที่แล้ว

    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

  • @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 :)

  • @robdorn420
    @robdorn420 7 ปีที่แล้ว +32

    Thanks Max, the best explanation I have ever got on this subject.

    • @academind
      @academind  7 ปีที่แล้ว +2

      Really amazing to hear that, thanks so much Rob!

    • @fishworld2021
      @fishworld2021 3 ปีที่แล้ว

      th-cam.com/video/SnXR6kX1SqA/w-d-xo.html try this

  • @Scout-uj1fj
    @Scout-uj1fj 5 ปีที่แล้ว

    Brilliant VDo...tysm :)

  • @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!

  • @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..

  • @mimoid7862
    @mimoid7862 5 ปีที่แล้ว +1

    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 :)

    • @academind
      @academind  5 ปีที่แล้ว

      Thank you, happy to read that you liked it :)

  • @Hadoitz
    @Hadoitz 5 ปีที่แล้ว

    Thanks man, your explanation has saved me from hours of headache.

    • @academind
      @academind  5 ปีที่แล้ว

      Really great to read that Hadyan :)

  • @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.

  • @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 :)

  • @sgt-en4orcer92
    @sgt-en4orcer92 5 ปีที่แล้ว

    Thanks, great explanation!!

  • @rohil3023
    @rohil3023 3 ปีที่แล้ว

    Great explanation.I think i understood most of what you said 👏

  • @karnpratapsingh8860
    @karnpratapsingh8860 2 ปีที่แล้ว

    awesome explanation!
    Really helpful. Thanks for it.

  • @muradprohere
    @muradprohere ปีที่แล้ว

    happy to learn from you mr max thank you for the explanation

  • @BeeshoStudying
    @BeeshoStudying ปีที่แล้ว

    thank you for the video and the simple explanation

  • @Incognito-ig7ld
    @Incognito-ig7ld 3 ปีที่แล้ว

    Thank you STRONG LESSON.

  • @michelleBSkiLLz
    @michelleBSkiLLz 2 ปีที่แล้ว

    nice and easy explanation! Thanks!

  • @bonzo6989
    @bonzo6989 3 ปีที่แล้ว +1

    Thanks bro it is really helpful. The only thing missing here is possiblity of using JSON to make a deep copy.

  • @nathanclassen8489
    @nathanclassen8489 4 ปีที่แล้ว

    Very helpful, very clear.

  • @boristheblad
    @boristheblad ปีที่แล้ว

    Nice to know that, well explained. thanks!

  • @nadiaharrismartinez
    @nadiaharrismartinez 2 ปีที่แล้ว

    That was so helpful. Great explanation. Thanks so much!

  • @mertciflikli
    @mertciflikli 4 ปีที่แล้ว

    This helped a lot. Thanks!

  • @lakhveerchahal
    @lakhveerchahal 3 ปีที่แล้ว

    Thanks as always Max

  • @abhishekshimpi214
    @abhishekshimpi214 2 ปีที่แล้ว

    Thank u so much dude u completely cleared my concept

  • @aayushikambriya3546
    @aayushikambriya3546 5 ปีที่แล้ว

    THE BEST EXPLANATION EVER MAX !

    • @academind
      @academind  5 ปีที่แล้ว

      Thanks so much Aayushi, this really means a lot to me!

  • @ramiahviknesh3014
    @ramiahviknesh3014 6 ปีที่แล้ว

    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.

    • @academind
      @academind  6 ปีที่แล้ว

      You're right, that's a great way of cloning an object or array

  • @poongudivanan
    @poongudivanan 5 ปีที่แล้ว

    Valuable information... !!!! Thanks great..

  • @TheSkis
    @TheSkis 7 ปีที่แล้ว

    Great explanation! Thorough and covered all feasible cases

    • @academind
      @academind  7 ปีที่แล้ว

      Thanks a lot for your nice feedback Ben!

  • @chrsbll
    @chrsbll 7 ปีที่แล้ว

    Extremely well explained. Thank you.

    • @academind
      @academind  7 ปีที่แล้ว

      Really happy to hear that, thanks so much Chris!

  • @OscarCedano
    @OscarCedano ปีที่แล้ว

    Thank u bro! This video really helped me!

  • @waseemansar4077
    @waseemansar4077 7 ปีที่แล้ว

    wow you make this concept very simple. Thanks Max

    • @academind
      @academind  7 ปีที่แล้ว

      Happy to hear that this was helpful! :)

  • @bhrugudesai
    @bhrugudesai 5 ปีที่แล้ว

    very well explained.

  • @adamscantley
    @adamscantley 7 ปีที่แล้ว

    This is absolutely fantastic. Thank you!

    • @academind
      @academind  7 ปีที่แล้ว

      Thanks so much Adam, I'm happy you're liking it!

  • @thilinamadushanka514
    @thilinamadushanka514 3 ปีที่แล้ว

    Thanks. It's just awesome.

  • @nitram_nosnibor
    @nitram_nosnibor 3 ปีที่แล้ว

    Maxi-baby you explained that REALLY well. Thank you, I totally get it now 👍🏻

  • @mc-ti5uq
    @mc-ti5uq 4 ปีที่แล้ว

    great explanation! vielen Dank :)

  • @aakritigoyal1432
    @aakritigoyal1432 3 ปีที่แล้ว

    U explained it so well❣💯

  • @degreman
    @degreman 2 ปีที่แล้ว

    Thats a Good explanation, thanks to you

  • @user-fj7bo8gh8e
    @user-fj7bo8gh8e 6 ปีที่แล้ว

    Awasome. The best explanation!

    • @academind
      @academind  6 ปีที่แล้ว

      Thank you so much, I'm glad you liked it!

  • @xseeginfo5064
    @xseeginfo5064 5 ปีที่แล้ว

    best primitive and reference tutorial forever

    • @academind
      @academind  5 ปีที่แล้ว

      Thank you, awesome to read that!

  • @djelasrca8091
    @djelasrca8091 ปีที่แล้ว

    Super useful. Thanks

  • @nathaliebenarroch
    @nathaliebenarroch ปีที่แล้ว

    very well done! thanks

  • @tymothylim6550
    @tymothylim6550 3 ปีที่แล้ว

    Thank you very much for this video! :) It was very interesting and I learnt a lot!

  • @emanfinding2603
    @emanfinding2603 6 ปีที่แล้ว

    Pretty clear, Thanks Max!

    • @academind
      @academind  6 ปีที่แล้ว

      Very happy to read that Eman, thank you for your comment!

  • @DharmendraSingh4ever
    @DharmendraSingh4ever 3 ปีที่แล้ว

    Thank you very much... You are great ✌🏼👍🏼

  • @petrockspiracy3120
    @petrockspiracy3120 5 ปีที่แล้ว

    That was a fantastic explanation and use of example.

    • @academind
      @academind  5 ปีที่แล้ว +1

      Thank you!

  • @frontend1662
    @frontend1662 7 หลายเดือนก่อน

    Amazing video, I'll recommend ❤

  • @HaifengZhu-pn3uq
    @HaifengZhu-pn3uq 3 หลายเดือนก่อน

    Max' content is always great

  • @trekmann123
    @trekmann123 7 ปีที่แล้ว

    Vielen dank für das tolle Video :)

    • @academind
      @academind  7 ปีที่แล้ว

      Freut mich, dass es gefällt :)

  • @rohancherry
    @rohancherry 6 ปีที่แล้ว

    excellent explanation, thank you Max.

    • @academind
      @academind  6 ปีที่แล้ว

      So happy to read that Ravi thank you very much!

  • @Brixsta503
    @Brixsta503 7 ปีที่แล้ว

    Thank you Max!
    You're the best

    • @academind
      @academind  7 ปีที่แล้ว

      Thanks for your amazing feedback Charles! :)

  • @gcg8187
    @gcg8187 11 หลายเดือนก่อน

    thank you, this was helpful

  • @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!

  • @syedhaiderraza9687
    @syedhaiderraza9687 5 ปีที่แล้ว

    Great, Clear my confusion. Thanks a lot

    • @academind
      @academind  5 ปีที่แล้ว +1

      That's really great to read, thanks a lot for your comment!

  • @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

  • @to-nd7yl
    @to-nd7yl 4 ปีที่แล้ว

    well done and thanks great explanation bro keep it up

  • @chinthanagunasekara7469
    @chinthanagunasekara7469 5 ปีที่แล้ว

    Thaks for the valuable information

  • @ericellison2413
    @ericellison2413 6 ปีที่แล้ว

    Holy shit max , you are amazing , i was confused about heap and stack for months , you are my man .....

    • @academind
      @academind  6 ปีที่แล้ว +1

      Really great to hear you're liking it - thank you so much! :)

  • @davida.7586
    @davida.7586 6 ปีที่แล้ว

    Very good explanation.
    Thank you!

    • @academind
      @academind  6 ปีที่แล้ว

      Great to read that you like it David, thank you!

  • @TechSquidTV
    @TechSquidTV 7 ปีที่แล้ว

    Amazing video as always. Sharing on Twitter!

    • @academind
      @academind  7 ปีที่แล้ว

      Awesome, thanks so 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! :)

  • @sharriceowens913
    @sharriceowens913 6 หลายเดือนก่อน

    thank you so much you have a new subscriber

  • @BrianGreigRWD
    @BrianGreigRWD 7 ปีที่แล้ว

    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.

    • @academind
      @academind  7 ปีที่แล้ว +1

      That's true, great addition! I'm happy you're liking it Brian!

  • @aabid3375
    @aabid3375 5 ปีที่แล้ว

    I am completely enjoying Your Nde.js course

    • @academind
      @academind  5 ปีที่แล้ว

      Happy to read that Abidul, thank you for your support!

  • @swapnilbhargava2927
    @swapnilbhargava2927 4 ปีที่แล้ว

    Max, you are simply awesome. I love you 😘