hey guys so there's a concept called
passed by value and pass by reference
which means if you pass a variable into
a function if you're passing by
reference then if you change the value
inside a function if the change gets
reflected to the variable outside and if
you pass by value then it actually makes
a copy and pass it into it so if you
change the value inside it doesn't get
reflected outside so how does that work
in JavaScript so let's look at it and
welcome to tech see tutorials in order
to understand we're gonna first create a
variable and pass it into a function and
change it the first we're gonna try
primitive value simple number and then
we're gonna try with object and see what
happens so let's do a primitive value so
let I'd say a equal to one so what is
the number a primitive value and I'm
going to create a function call that
change equal to it's an arrow function
and each side
it takes a some sort of argument called
Val and inside it would simply do this
well equal to two so no matter what you
pass to this function it will reassign
it a different value which is two now
let's pass a into change so I would say
change and pass a and then after that
let's console.log see if it preserves
that Valley or its value change from one
to two if I run this I would get one
does this really means that it is being
passed by value because when you pass by
value it will make a copy and
if you change inside the outside would
not get reflected in JavaScript it's
actually always passed by a reference
and I'll prove it why that's true a lot
of people say it's actually when you
pass a primitive value it's passed by by
value and when you pass an object is
passed by reference but that's not true
and in JavaScript it's always passed by
reference so let's look at an object and
do the same thing and see what happens
so here instead of a I'm going to have
an object and it will have num1 okay so
this object has a property called num
and its value is 1 and here inside I am
just going to empty the object and so
what happens is when I pass a inside it
should reassign a value to completely
new object which is an empty object okay
when I run it it still gives me num
equal to 1 so it hasn't really changed
its value does this mean is still passed
by a value and it was actually passed by
reference and in order to prove it
instead of reassigning value all I have
to do is if I do well dot num equal to 2
so instead of replacing the entire
object with an empty object I'm just
going to mutate it there's a difference
between mutating and reassigning when I
look at that in a detail but let's look
at what really happens
so before you used to give me num as 1
now let's look at what it does actually
it gives me num equal to 2 which means
it is being passed by reference and that
is why when I changed when I mutated
that mutation also is reflected in a
that's why it's to insert of one so why
when I did this it did not reflect so in
JavaScript what happens when you assign
a value to a variable it actually holds
the address of where this is stored so
it has a reference to the address right
now when I pass this a into this change
function it's actually passing the
reference but when I use an equal sign
which means I am actually reassigning
the value it actually changes the
address and as if like it's recreating
this same variable again
because the scope is different it
actually creates a variable called Val
inside when I reassign it and that is
the main difference and that is why the
val is now empty object and a which is
completely different object is num equal
to one now when I did this when it was a
primitive value what really happened is
primitive values are you can mutate
primitive value you only have to you can
only replace it so when I replace it's
actually passing by a reference but
inside when I replace it it creates a
new variable so it is passed by
reference but it just creates a variable
inside this new scope which is this
function alright to look investigate
further now let's look at this let's
create a object which is num as one and
if I do let be equal to a so now a B
should have reference to a so if I
change into a it should reflect into B
right so let's first console.log them
both a here
I can console.log and if I run it
they're both same now if I do a dot num
equal to 2 what should happen when I run
it because it is B is still linked to a
it's also changing B as you can see
however if I reassign it so if I say a
equal to an empty object now if I run
this I would get different results so
the reassignment is actually sort of you
lose at reference so in nutshell when
you use equal sign it's basically change
if the variable would change its address
regardless of its if it's a primitive
type or object and when you mutate the
object it will keep its reference
because the same object but in in
primitive types you cannot really mutate
it so that's why you were seeing what
you okay so that's it I hope you like
this video and if you did please like
subscribe and provide a constructive
comment and also if you can translate
this video from me in your native
language so your countrymen can also
enjoy basically you have to provide a
closed captioning I'll provide a link to
the closed captioning so the link so you
can go there and add the translation
there I provide a link in the
description and if you don't know how to
do it
please ping me and I can help you I
provide English closed captioning to
most of my video but I don't have enough
time to translate in a lot of other
languages and I don't know a lot of
languages so if you can do it that'll be
that'd be great
also another thing one of my friends kid
who is only like 6 years old he has made
his first wall actually a second youtube
video and he gave me a link to watch it
and I just want to promote it so that
he gets low kudos and he gets motivation
to make more how to play chess videos
and he's trying his best he the video
make already may not be the best but if
you can if you can please watch for a
few minutes that would make him happy
I'll provide a link here
thank you