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 Рік тому +8

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

  • @edwardm3552
    @edwardm3552 Рік тому +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 Рік тому +3

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

  • @faizanahmed9304
    @faizanahmed9304 Рік тому +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 Рік тому +3

    Thank you for the video on this key topic!

  • @NedumEze
    @NedumEze Рік тому +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!

  • @muhammadijaz6042
    @muhammadijaz6042 5 місяців тому

    Excellent explanation 👍

  • @TheNotoriousJS
    @TheNotoriousJS 6 місяців тому

    really good explanation. Thank you!!!

  • @hertechera
    @hertechera 10 місяців тому

    Thank you so much for this! Appreciate it so much

  • @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  Рік тому

      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.

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

  • @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 - ua-cam.com/video/0k4NwimfszA/v-deo.html
      localStorage - ua-cam.com/video/hOCYNdgsUfs/v-deo.html
      Messaging - ua-cam.com/video/n2NeCLTUMTE/v-deo.html (first video in the playlist)
      structuredClone - video coming soon

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