JavaScript Value vs Reference Types

Поділитися
Вставка
  • Опубліковано 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

КОМЕНТАРІ • 70

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

  • @lynnqi6451
    @lynnqi6451 4 роки тому +6

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

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

  • @ceddh15
    @ceddh15 3 роки тому +3

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

  • @aaabbb-qv4ff
    @aaabbb-qv4ff 4 роки тому

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

  • @Navistar-cu2po
    @Navistar-cu2po Рік тому

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

  • @EnglishRain
    @EnglishRain 4 роки тому

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

  • @han5173
    @han5173 4 роки тому

    very straightforward and easy to understand! 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.

  • @sdix20
    @sdix20 Рік тому

    Excellent, concise explanation. Thank you, Mosh!

  • @DS-zr9gv
    @DS-zr9gv 4 роки тому

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

  • @nourslimane571
    @nourslimane571 2 роки тому

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

  • @ablamohamedamine
    @ablamohamedamine 4 роки тому

    Very useful Tutorial !
    Thanks a lot :D

  • @11vag
    @11vag 4 роки тому +1

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

    • @andTutin
      @andTutin Рік тому

      the thing is. its wrong

  • @mariekevandijk4
    @mariekevandijk4 2 роки тому

    Brilliantly explained, thanks!

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

  • @sakthi3687
    @sakthi3687 6 років тому

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

  • @rohitkudalkar92
    @rohitkudalkar92 4 роки тому

    ooohhh, That was a very clear example. Thanks.

  • @ahmedabbas8774
    @ahmedabbas8774 5 років тому

    love you your videos very good explanation.

  • @abdelkrimeljaouhari2370
    @abdelkrimeljaouhari2370 Рік тому

    you've made it very simple ,thanks.

  • @a_maxed_out_handle_of_30_chars

    thank you, simple and to the point :)

  • @danielakoyleek3757
    @danielakoyleek3757 Рік тому

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

  • @jsprodev2425
    @jsprodev2425 6 років тому

    Very helpful. Thumbs up

  • @ciceroaraujo5183
    @ciceroaraujo5183 4 роки тому

    You are really a great professor

  • @Dj7wiLLY
    @Dj7wiLLY 4 місяці тому

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

  • @Antarah525
    @Antarah525 3 роки тому

    Thanks, This really helped 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?

  • @tanvirmohammedakif5899
    @tanvirmohammedakif5899 2 роки тому

    concept nicely explained, highly appreciated ///

  • @AA-fe1zl
    @AA-fe1zl 4 роки тому

    Excellent video!

  • @ajaynathgupta
    @ajaynathgupta 6 років тому

    Nice explanation

  • @s7s_space
    @s7s_space 3 роки тому

    thanks Mosh

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

  • @rafaelcorrea7959
    @rafaelcorrea7959 6 років тому

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

  • @drementer9
    @drementer9 Рік тому

    🔥 awesome content

  • @bavan1358
    @bavan1358 2 роки тому

    I almost gave up on this topic , you saved me

  • @behindthescene2727
    @behindthescene2727 2 роки тому

    Thank you sir

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

  • @baatar
    @baatar 4 роки тому

    You are great.

  • @sonamuhialdeen7866
    @sonamuhialdeen7866 3 роки тому

    So good tutorial

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

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

  • @javascript_developer
    @javascript_developer 3 роки тому

    Thank again.

  • @KayhanEskanndari
    @KayhanEskanndari Рік тому

    best explain ever

  • @doniaelfouly4142
    @doniaelfouly4142 12 днів тому

    Thanks

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

  • @PeriklesPeriklesoglu
    @PeriklesPeriklesoglu Рік тому

    thanks

  • @mindurownbussines
    @mindurownbussines 5 років тому

    in

  • @mohamedprogrammer19
    @mohamedprogrammer19 Рік тому

    im happy cuuuuz i find this video

  • @LexRexLibertarian
    @LexRexLibertarian 3 роки тому

    Great video but the scope pollution part confused me a bit

  • @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 3 місяці тому

      Visual Studio Code

  • @kayan_dev
    @kayan_dev 4 роки тому

    let scoop is out of function increase

  • @samareshshil69
    @samareshshil69 Рік тому

    great

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