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
Wow, I didn't know that. I really need to improve my JS.
My family is from Hamedan...Love your explanation of this topic really simplified, clean, and straight to the point.
No matter the amount of articles you have read,Mosh will always teach you something new and useful.Awesome teacher,thanks Mosh.
great explanation, very clear, straight forward. Help me a lot, thank you very much!
value vs reference was driving me crazy but this video absolutely helped me. thank you for the explanation
Clear, Concise, Straight to the point. Your video helped me grasp the concept quickly.
very straightforward and easy to understand! thank you!
FANTSTIC explanation! You're brilliant man! Hit home in first stroke!
VERY nice explanation. Simple and to the point. Thank you!
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.
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
Excellent, concise explanation. Thank you, Mosh!
Brilliantly explained, thanks!
this is excellent, im reading a "well regarded" book at the moment and its like double dutch compared to this simple explanation... thanks mate.
So far, the best explanation I´ve seen about this. Thank you mate.
the thing is. its wrong
you've made it very simple ,thanks.
thank you, simple and to the point :)
this explanation helps with every programming language , also one can learn about this fundamentally with C , great content.
ooohhh, That was a very clear example. Thanks.
love you your videos very good explanation.
You are really a great professor
Very helpful. Thumbs up
Thanks sir. This was hard to get my head around til i watched this.
Very useful Tutorial !
Thanks a lot :D
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!
Brasileiro tá em td lugar kkkk
Why are you so awesome at explaining. Please make more on Angular7 and JS
Thanks, This really helped me
Excellent video!
Nice explanation
explained SOOOOOOOOOOO DAMN WELL !!!!!!!!!!!! Thank you so much, man !!!
Thanks Mosh 👍✌️😊... I am expecting ES6 features video
thanks Mosh
concept nicely explained, highly appreciated ///
Thank you sir
So good tutorial
Thank again.
You are great.
🔥 awesome content
best explain ever
I almost gave up on this topic , you saved me
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?
thanks
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!
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?
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".
How do I force copying an object? (not by reference)
im happy cuuuuz i find this video
great
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?
refrence coz y is coping an object
reference i think. that is object now
Great video but the scope pollution part confused me a bit
So much better than my explanation 😅
in
let number = 10;
function inc(n) {
number = number + n;
}
inc(5);
number;
// result is 15
HOW DOES THAT HAPPEN?
In case some else gets confused by this. see 0:34 of the video. This is by definition pass by reference.
let scoop is out of function increase
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.
Visual Studio Code
❤❤
🔥🔥🔥🔥🔥🔥
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 😢😢
Unfortunately I don't understand the concept :(
String is considered as an object and primitive type at the same time how confusing is that
🤣🤣🤣🤣🤣🤣
confused unga bunga
wrong