There are fundamentally two data types in Javascript:
1๏ธโฃ Primitive data type
2๏ธโฃ Reference data type ( or non primitive)
Now, let's look at each one of them in detail......๐
1๏ธโฃ Primitive data type
2๏ธโฃ Reference data type ( or non primitive)
Now, let's look at each one of them in detail......๐
๐ Primitive data type:
โก๏ธ A primitive data type is one where the size of data type is fixed.
๐ Ex - When we say let num = 2; the computer creates a finite space in RAM to store the value
๐ช When we update num to some other value, it directly updates this stored data.
โก๏ธ A primitive data type is one where the size of data type is fixed.
๐ Ex - When we say let num = 2; the computer creates a finite space in RAM to store the value
๐ช When we update num to some other value, it directly updates this stored data.
๐ฏ Also, in the case of primitive data types, the computer accesses the value in the memory location directly.
๐ Common examples of primitive data types are: Boolean, Number, String etc
๐ Common examples of primitive data types are: Boolean, Number, String etc
๐ Reference data type:
โ Unlike primitive data type, when we create a variable to store a reference data type, the address of the data is stored in the variable and not the data itself.
Let me explain this a bit more...๐
โ Unlike primitive data type, when we create a variable to store a reference data type, the address of the data is stored in the variable and not the data itself.
Let me explain this a bit more...๐
๐ฏ When we create reference data type variable, as:
let myObj = {name: 'Sumit'}
๐ก๏ธ Here 'myObj' doesn't have the object itself.
โก๏ธ It, rather, points to a memory location where the actual object (with name:'Sumit') is stored.
let myObj = {name: 'Sumit'}
๐ก๏ธ Here 'myObj' doesn't have the object itself.
โก๏ธ It, rather, points to a memory location where the actual object (with name:'Sumit') is stored.
๐ก Contrast this with 'num' variable above which directly stores the value.
๐ So, primitive data type directly store the value whereas reference data type store the value's address in memory and not the value itself.
๐ So, primitive data type directly store the value whereas reference data type store the value's address in memory and not the value itself.
โ
This is why they're called 'reference' as the address stored serves as a reference to the actual value.
๐๏ธ Also, primitive data types have fixed sizes while reference data types' size is dynamic.
๐๏ธ Also, primitive data types have fixed sizes while reference data types' size is dynamic.
I write threads daily on web dev so if you enjoyed this thread, please:
โ Follow me @sumitsaurabh927 for more of these.
โค๏ธ Each like, every comment of y'all motivates me to come up with awesome new stuff!
โ RT the tweet below to show me some love.
โ Follow me @sumitsaurabh927 for more of these.
โค๏ธ Each like, every comment of y'all motivates me to come up with awesome new stuff!
โ RT the tweet below to show me some love.
Loading suggestions...