Difference between Primitive and Reference Type Data Type in JavaScript | Data types in JavaScript

preview_player
Показать описание
Difference Between Reference Type and Primitive type Data type.

in JavaScript mainly 5 types of Primitive Data Type are

String Number Boolean Undefined and Null

JavaScript has 3 Reference Type data types

Array Object and Function

Primitive type occupied fixed size in memory.

in reference type we can add or delete the elements in it ,so size varies according to values.

if a variable is initialized as a primitive type, and assign that variable to a another Variable , the new Variable holds the copy of the value .

like in this example we assigned 10 to variable x and in y variable we have assigned x;

so now if we change the value of x as 11 and print the both variable in console we get x = 11 and y = 10;

so y store the value of x as 10 at different location in memory and we changed the value of x didn't effect the value of y.

If a Variable initialized as a reference type , and assign that variable to a another Variable ,new Variable holds the reference or the address of the first variable .

like in this example we have initialized arr1 as array and value 1,2 and 3.

and we have assigned this arr1 to arr2,

now if we change the value of arr1 we add the 4 at last .

and we print both array we get 1,2,3 and 4 .

because arr2 only points the address of arr1 values stored in memory if we change arr1 value then arr2 will also show that changes.

Thank You

please like and subscribe.
Рекомендации по теме
Комментарии
Автор

thank you ... if was short but brief ♥

hehswhs