Javascript pass by Value vs pass by Reference tutorial

preview_player
Показать описание
Does JavaScript use pass by reference or pass by value when i pass a variable to a function ( Primitive or Object ).

Article and code samples from this tutorial

Please be my patreons on patreaon

Follow me for technology updates

Help me translate this video.
Рекомендации по теме
Комментарии
Автор

Just bcoz of ur all videos, I was able to answer all JS questions for my job interview and finally got and good job as Frontend developer. Thank you so much bro :)
I have given your channel link to my friends who asked me about JS.

amiturfnd
Автор

@techsith: Great video as usual. I wanted to add some details for the benefit of the learners here:

Difference between Primitives & Objects in Javascript, is mainly is that, the first category of datatypes are passed by value and later ones are passed by reference. Both are NOT passed by reference. Only objects are passed by reference. Reason being, objects are complex data types and hence stored in HEAP memory instead of STACK (where primitive values are kept for faster access).

For objects only a reference is kept on the stack. So inside the function, when you try to access it using val.num, you are actually accessing the same memory location in HEAP. Hence anything done to val.num will change the original data in the HEAP, hence affecting a.num. The exception to this is when you "re-assign". That as @techsith mentioned creates a new instance (on the HEAP) and no more linked value of object a in memory. So when you re-assigning there are 2 independent objects in stack pointing to two different memory locations in heap.

On the contrary for something like a primitive (say a = 1), when we assign b=a, a new variable is created with a copied value. So now b and a are completely separate and have no relation at all.

Hope this helps.

sidheart
Автор

In JavaScript arguments are always being passed by value. The reason for the reflection of reference types is because the parameter will contain the value of the reference type, which itself is the reference of the variable.

aninaslyan
Автор

It's weird that after 8h at work I still have the need to surround myself with JavaScript :D

wojciechlipinski
Автор

This was really great. It's amazing how many people online get this wrong. You just cleared it for me. Thanks again.

thatoyaonebogopa
Автор

I love that all your explanations are plain and simple. Defintely some of the bes JS videos on Youtube!

moy
Автор

Thank you! Helped me understand the odd behavior I was noticing in my code.

snwdn
Автор

Sacred knowledge given with humbleness. thank you.

bhrugudesai
Автор

I'm addicted to your videos, man!

thaysdosSantosneves
Автор

Hi, thanks for your explanation. I definitely agree with you that primitives seem actually like they are also passed by reference, why is this not the common belief? Also what about the people who say that primitives are stored on the stack and objects on the heap? Is this just a concept or is there something to this? Thanks

aquilaapologetics
Автор

You saved me! I tried for an hour with other methods, with no luck! Your method works!!

sunny
Автор

omg even mdn got this concept wrong and you got it right....brilliant job. it is much easier to understand if you know pointers. i'm gonna watch every video in your channel related to js. thank you

prasoonchatterjee
Автор

This video is INCORRECT. In JS, it's always pass by value. Always. It seems like objects are being passed by reference, but in reality, they are being passed by value.

petesampras
Автор

I think it's pass by value . The references are itself passed by value. Java also works this way.

MrAdi
Автор

Having very good experience in JS but while going through ur tutorials seems like .... I only crossed the river Ocean is still there :D wonderful tutorial

a_l_o_k_
Автор

You are the man!
Helped me more than other channels combined.
Thank you.

ReinaldoTrindade
Автор

Well explained..this question was to me in an interview and he said JS is pass by value..this video I have not seen before otherwise I would have explained him..

rajeshsahu
Автор

Primitives are always passed by value, objects are passed by reference. If we reassign a primitive or object inside a function its scope is bound to function only.

RamaKrishna-jdwm
Автор

I can also give good reply to my java script interview question, last month Nov/December 2017, I have learnt Angular 5 and still learning Java script every day. By watching your video I can crack two interviews as full stack developer, day by day your videos give me more clarification in Javascript/ Angular 5 .

somesbhowmick
Автор

Then why we need deepClean techniques, reassign should solve the problem right?

pratik_N