JavaScript Value vs Reference Types

Поделиться
HTML-код
  • Опубликовано: 14 май 2018
  • 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:
    programmingwithmosh.com
    / programmingwithmosh
    / moshhamedani

Комментарии • 69

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

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

  • @lynnqi6451
    @lynnqi6451 4 года назад +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.

  • @han5173
    @han5173 4 года назад

    very straightforward and easy to understand! thank you!

  • @EnglishRain
    @EnglishRain 4 года назад

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

  • @aaabbb-qv4ff
    @aaabbb-qv4ff 4 года назад

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

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

  • @sdix20
    @sdix20 Год назад

    Excellent, concise explanation. Thank you, Mosh!

  • @mariekevandijk4
    @mariekevandijk4 2 года назад

    Brilliantly explained, thanks!

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

  • @11vag
    @11vag 4 года назад +1

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

    • @andTutin
      @andTutin Год назад

      the thing is. its wrong

  • @abdelkrimeljaouhari2370
    @abdelkrimeljaouhari2370 Год назад

    you've made it very simple ,thanks.

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars Год назад +1

    thank you, simple and to the point :)

  • @nourslimane571
    @nourslimane571 2 года назад

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

  • @rohitkudalkar92
    @rohitkudalkar92 4 года назад

    ooohhh, That was a very clear example. Thanks.

  • @ahmedabbas8774
    @ahmedabbas8774 5 лет назад

    love you your videos very good explanation.

  • @ciceroaraujo5183
    @ciceroaraujo5183 4 года назад

    You are really a great professor

  • @jsprodev2425
    @jsprodev2425 6 лет назад

    Very helpful. Thumbs up

  • @danielakoyleek3757
    @danielakoyleek3757 Год назад

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

  • @ablamohamedamine
    @ablamohamedamine 4 года назад

    Very useful Tutorial !
    Thanks a lot :D

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

  • @DS-zr9gv
    @DS-zr9gv 4 года назад

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

  • @Antarah525
    @Antarah525 3 года назад

    Thanks, This really helped me

  • @AA-fe1zl
    @AA-fe1zl 4 года назад

    Excellent video!

  • @ajaynathgupta
    @ajaynathgupta 6 лет назад

    Nice explanation

  • @Dj7wiLLY
    @Dj7wiLLY 3 месяца назад

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

  • @sakthi3687
    @sakthi3687 6 лет назад

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

  • @s7s_space
    @s7s_space 3 года назад

    thanks Mosh

  • @tanvirmohammedakif5899
    @tanvirmohammedakif5899 2 года назад

    concept nicely explained, highly appreciated ///

  • @behindthescene2727
    @behindthescene2727 2 года назад

    Thank you sir

  • @sonamuhialdeen7866
    @sonamuhialdeen7866 3 года назад

    So good tutorial

  • @javascript_developer
    @javascript_developer 3 года назад

    Thank again.

  • @baatar
    @baatar 4 года назад

    You are great.

  • @drementer9
    @drementer9 Год назад

    🔥 awesome content

  • @KayhanEskanndari
    @KayhanEskanndari Год назад

    best explain ever

  • @bavan1358
    @bavan1358 2 года назад

    I almost gave up on this topic , you saved me

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

  • @PeriklesPeriklesoglu
    @PeriklesPeriklesoglu Год назад

    thanks

  • @ArgKilljoy
    @ArgKilljoy Год назад

    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!

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

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

  • @rafaelcorrea7959
    @rafaelcorrea7959 6 лет назад

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

  • @mohamedprogrammer19
    @mohamedprogrammer19 Год назад

    im happy cuuuuz i find this video

  • @samareshshil69
    @samareshshil69 Год назад

    great

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

  • @LexRexLibertarian
    @LexRexLibertarian 3 года назад

    Great video but the scope pollution part confused me a bit

  • @codeconceptswithmughees
    @codeconceptswithmughees Год назад

    So much better than my explanation 😅

  • @mindurownbussines
    @mindurownbussines 5 лет назад

    in

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

  • @kayan_dev
    @kayan_dev 4 года назад

    let scoop is out of function increase

  • @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 2 месяца назад

      Visual Studio Code

  • @igu642
    @igu642 Год назад

    ❤❤

  • @prateek4524
    @prateek4524 3 года назад

    🔥🔥🔥🔥🔥🔥

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

  • @mek059
    @mek059 5 лет назад

    Unfortunately I don't understand the concept :(

  • @mehmetedex
    @mehmetedex 3 года назад +1

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

  • @andTutin
    @andTutin Год назад

    wrong