Understanding Passing by Reference or Value in JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ก.ค. 2022
  • This video tutorial explains the difference between passing by reference and passing by value in Javascript. Also discussed are the concept of passing and Primitive vs Object datatypes.
    Code from Video: gist.github.com/prof3ssorSt3v...

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

  • @suelembezerra22
    @suelembezerra22 2 ปีที่แล้ว +8

    Finally soone who speaks in a calm way and getting to the point asap

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

    People who are wanting to fully understand JavaScript should go to this guy. You are so great at explaining things simply. Thank you.

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

    You always strive to pass understanding in your teaches, not just knowledge
    Thank you sir !!!!

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

    Thank you sir. I truly appreciate your hardwork and effort that you're putting to explain us.

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

    Well explained, I came here from the previous passing by reference video by yours. Everything settled down in my mind after getting them one after another. Thank you sir.

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

      Endless, which video is the previous one?

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

      @@LearnEnglishwithKevin101 I couldn't find the video I was talking but nothing much different from this one. Maybe he replaced to this one.sry

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

    Thank you for the video on this key topic!

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

    So helpful. Thank you sir

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

    this was exactly what I needed to understand passing by reference. thank you for taking the time to do this.

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

    Thank you Professor! This is the best explanation that made it clear for me and I've seen many!

  • @debhume4474
    @debhume4474 2 หลายเดือนก่อน

    This was really helpful! Thank you!

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

    Thank you so much for this! Appreciate it so much

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

    really good explanation. Thank you!!!

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

    Excellent explanation 👍

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

    Thanks for this great explanation ☺️

  • @Hussain-yv2xq
    @Hussain-yv2xq ปีที่แล้ว

    Thank You sir great explanation

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

    Thanks Steve, it helped me a lot getting through this subject. I'm currently on a learning path with Codecademy (which should learn from you) and Mosh Hamedani, but I'm sure when I'm done I will watch all of your videos.
    One question though: why did you use {} when logging the param?
    Thank you!

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  ปีที่แล้ว +1

      Inside console log you can get different versions of objects when you wrap the variable with { }. The { } will turn things like HTML elements into Objects with properties.

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

    Thank you sir, hope you make a typescript tutorials.

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

    Ah thank you steve, i am new to js and just meet this problem, but how can i copy the value to new object?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  ปีที่แล้ว

      If the value is a Primitive then the value will be copied. If the value you want to copy is an Object then it is only a reference that is copied.
      To create a copy of an object you can convert it to a JSON string, or pass it through localStorage, or pass it through a Message, or you can recursively loop through the object making copies of all the primitives inside the object and copying them into a new object, or there is a new method called structuredClone.
      JSON - th-cam.com/video/0k4NwimfszA/w-d-xo.html
      localStorage - th-cam.com/video/hOCYNdgsUfs/w-d-xo.html
      Messaging - th-cam.com/video/n2NeCLTUMTE/w-d-xo.html (first video in the playlist)
      structuredClone - video coming soon

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

    The main difference between C and JavaScript is that in C, we explicitly pass the memory address of a variable to the function using pointers to achieve pass by reference, whereas in JavaScript, we do not explicitly pass a memory address, but rather pass a reference to the original object by value. So I think we do not have pass by reference in JS, all we have is pass a reference.

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

      Correct! JavaScript only has “pass by value”. It seems that some of the confusion stems from that those values are references to objects or arrays.
      In fact I find the distinction between objects and primitives a bit detrimental. It would make more sense to think of mutable vs immutable objects. A number cannot be changed, when a variable is set = another number, it doesn’t change the number, but just like setting a variable = another object, changes what the variable points to.
      Note that every time someone says “it is a copy, so we don’t modify the original,” they set the variable = something new, and when they say “it is a reference, so we can modify the original” they use . to change some property or call a method on the object, that makes it mutate itself.
      There are no pass by reference in neither Java nor JavaScript.

  • @webb-developer
    @webb-developer 5 หลายเดือนก่อน

    done ☑

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

    I believe there is a little fault in the logic of this demonstration as the "num" you're logging out on line 35 is not the "num" you are passing into the function on line 40, but is simply the "num" variable declared on the global scope. To prove it, if you pass into the function some other number variable, you will get the same console result... :)

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  ปีที่แล้ว

      Yes. num is the global variable. The function is demonstrating that we are NOT changing the original variable - num. param is a new variable that has a copy of num.