JavaScript Value vs Reference Types

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ต.ค. 2024
  • JavaScript Value vs Reference Types
    🔥Get the COMPLETE course (83% OFF - LIMITED TIME ONLY): bit.ly/2M1sp4B
    Subscribe for more videos:
    / @programmingwithmosh
    Want to learn more from me? Check out my blog and courses:
    programmingwith...
    / programmingwithmosh
    / moshhamedani

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

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

    No matter the amount of articles you have read,Mosh will always teach you something new and useful.Awesome teacher,thanks Mosh.

  • @NoahNobody
    @NoahNobody 6 ปีที่แล้ว +19

    Wow, I didn't know that. I really need to improve my JS.

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

    My family is from Hamedan...Love your explanation of this topic really simplified, clean, and straight to the point.

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

    Another underlying principle worth mentioning here for the example starting at 3:05 is called "variable shadowing". Because our inner variable's name is the same as the outer variable's, a variable in the inner scope is said to shadow a variable in the outer scope. That is why we cannot re-assign a value assigned to a global variable.
    However, if you tweak this example a little bit:
    let number = 10;
    function increase() {
    number++;
    }
    increase();
    console.log(number);
    So, here we are not passing in any arguments and therefore a value in the global variable can be re-assigned.

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

      THANK YOU SO MUCH!!!! I researched a lot and the answer was right here in the comments! I was testing:
      *console.log('obj ',increase(obj));*
      *increase(obj);*
      and was getting undefined. I couldn't understand why... actually I don't fully understand yet. Because if I change:
      *function increase(x){ x++; }*
      *increase(obj);*
      *console.log('obj ', obj);* -> still gets 10.
      and *console.log('obj ',increase(obj));* -> still gets undefined
      is it because when we put "increase(obj);" obj becomes the x inside de function so we get the "variable shadowing" again?
      also... why do that function at all if it will only be able to deal with that one variable? it is easier to just put *obj++;* then print

  • @lynnqi6451
    @lynnqi6451 5 ปีที่แล้ว +6

    great explanation, very clear, straight forward. Help me a lot, thank you very much!

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

    value vs reference was driving me crazy but this video absolutely helped me. thank you for the explanation

  • @Navistar-cu2po
    @Navistar-cu2po ปีที่แล้ว

    Clear, Concise, Straight to the point. Your video helped me grasp the concept quickly.

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

    Coming from C# to this I wasn't able to reference correctly a variable in JS. Thank You SO MUCH! Now I get it with this video!

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

      Brasileiro tá em td lugar kkkk

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

    So far, the best explanation I´ve seen about this. Thank you mate.

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

      the thing is. its wrong

  • @robertjones811
    @robertjones811 6 ปีที่แล้ว +4

    Javascript is always pass by value. Pass by reference does not exist in JavaScript. When you remind yourself about the fact that an object and a reference to that object are 2 *independent* things, it will make more sense. I suppose we've been conflating and object "reference" and passing function arguments "by reference."
    stackoverflow.com/a/13104500/463206
    stackoverflow.com/questions/37290747/pass-by-reference-javascript-objects#comment62108759_37290747
    When passing a object reference, the *value* of that reference (e.g. the address of the object) is copied and that copy is passed - This is the definition of pass by value. "Pass by reference" means the original reference, not a copy, is passed. Here's the litmus test: In the called function assign a different object to the reference variable. If the original reference now points to this different object then you have pass by reference. But this does not happen in JavaScript.
    Of course you can change the object's properties, because the reference copy is pointing to the same object. But that is not germane. Rather, "Pass by xxxx" means - whatever you're passing, if it a copy it is "by value", if it is the original it is "by reference".

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

    Thanks sir. This was hard to get my head around til i watched this.

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

    FANTSTIC explanation! You're brilliant man! Hit home in first stroke!

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

    this explanation helps with every programming language , also one can learn about this fundamentally with C , great content.

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

    Excellent, concise explanation. Thank you, Mosh!

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

    thank you, simple and to the point :)

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

    explained SOOOOOOOOOOO DAMN WELL !!!!!!!!!!!! Thank you so much, man !!!

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

    you've made it very simple ,thanks.

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

    Brilliantly explained, thanks!

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

    very straightforward and easy to understand! thank you!

  • @aaabbb-qv4ff
    @aaabbb-qv4ff 4 ปีที่แล้ว

    VERY nice explanation. Simple and to the point. Thank you!

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

    this is excellent, im reading a "well regarded" book at the moment and its like double dutch compared to this simple explanation... thanks mate.

  • @DS-zr9gv
    @DS-zr9gv 4 ปีที่แล้ว

    Why are you so awesome at explaining. Please make more on Angular7 and JS

  • @mel7im19911
    @mel7im19911 4 ปีที่แล้ว +2

    What about if we define Number/Strings as Objects, ex:
    let x = new Number (10)
    let y = x
    Is it will be assigned by reference or value?

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

      refrence coz y is coping an object

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

      reference i think. that is object now

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

    i saw your video it was so interesting I thought it would be boring like other videos of other programers but when I started to see it the 6 mins gone away so fastly that I saw your video again and again and it was so fun can u make a video on symbols es6?

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

    Great explanation. I wish you wouldn't have named the parameters the same as the arguments. I would've understood it a bit sooner. Thank you!

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

    You are really a great professor

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

    I almost gave up on this topic , you saved me

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

    ooohhh, That was a very clear example. Thanks.

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

    Thanks, This really helped me

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

    A doubt - when we're consoling the value of x which is supposed to store the reference (address) of the object then why it is printing the object itself? Should not it print the address of the object as it is storing the same?

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

    Very useful Tutorial !
    Thanks a lot :D

  • @HassanMalikTW
    @HassanMalikTW 6 ปีที่แล้ว +2

    let number = 10;
    function inc(n) {
    number = number + n;
    }
    inc(5);
    number;
    // result is 15
    HOW DOES THAT HAPPEN?

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

      In case some else gets confused by this. see 0:34 of the video. This is by definition pass by reference.

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

    Thanks Mosh 👍✌️😊... I am expecting ES6 features video

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

    love you your videos very good explanation.

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

    concept nicely explained, highly appreciated ///

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

    thanks Mosh

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

    Thank you sir

  • @AA-fe1zl
    @AA-fe1zl 4 ปีที่แล้ว

    Excellent video!

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

    So good tutorial

  • @doniaelfouly4142
    @doniaelfouly4142 3 หลายเดือนก่อน

    Thanks

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

    best explain ever

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

    Very helpful. Thumbs up

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

    Nice explanation

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

    Thank again.

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

    Great video but the scope pollution part confused me a bit

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

    How do I force copying an object? (not by reference)

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

    🔥 awesome content

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

    You are great.

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

    So much better than my explanation 😅

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

    Can anybody tell me what software or site he is using to demonstrate this? I am new to this and would really benefit from an app or something that lets you practice.

    • @ZTP1940
      @ZTP1940 5 หลายเดือนก่อน

      Visual Studio Code

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

    im happy cuuuuz i find this video

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

    let scoop is out of function increase

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

    You should have given numbers for videos 😢😢 it's hard to find which is first and next video ... Content is good but navigation is hard 😢😢

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

    String is considered as an object and primitive type at the same time how confusing is that
    🤣🤣🤣🤣🤣🤣

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

      confused unga bunga

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

    great

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

    Unfortunately I don't understand the concept :(

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

    ❤❤

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

    🔥🔥🔥🔥🔥🔥

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

    wrong

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

    thanks