Sunday, 6 January 2019

JavaScript Variable Types

Variable Types

In JavaScript each variable you define will be assigned a type which then belongs to its own Object type and gives you access to type specific properties and methods. However not only do your variables have a data type they also have an access type. The data type determines what data your variable is defined as and therefore influences what properties and methods it has whereas the access type dictates how the variable is accessed and modified. 

JavaScript has 7 variable data types which are Number, String, Boolean, Symbol, Object, Null and Undefined. These variables also have one of two different memory types which are Primitive and Reference. Now I know you might be thinking "So where are Function and Array variables?" well these variables are defined as part of Object whereby a function is seen as an object that is callable and an array is seen as representation of a list or set that inherits from the Array.prototype object.

This raises the next point which people beginning JavaScript development may not grasp in that every variable in JavaScript is essentially an object that inherits from a particular types prototype which then gives these variables access to specific properties and methods. Lets take the Number type as an example, once you have defined a variable as a number you have access to built in Number.prototype methods such as parseInt(), toFixed() and isNaN(). Below I will demonstrate some variable types and their type.prototype inherited methods.



Now the second part regarding variable types is key to understanding future topics such as immutability which will be discussed in a future post soon. So when we say a variable is a primitive or reference type what we mean is it is either a variable that is directly accessed and modified by its value whereas a reference type is a type that simply points to a space in memory where values are stored and when you access this variable you are accessing its reference to where its stored in memory.

In JavaScript Objects are reference types and everything else is a primitive. A good way to explain the difference between primitive and reference types is to show the difference when trying to copy values to a new variable. If you create a number and then assign its value to a new number variable you then have two variables with the same number value however when you change one of their values only that variable is changed as it only cares about its own stored value being accessed and modified. With reference types however if you create an object and then assign another variable to the same value you have created two variables that due to reference type point to the same object that is stored in memory, therefore when you go to change a property in this object from either variable both will change as they are pointing to the same object in memory. Below is a quick demo of this.


This concludes the first blog for JavaScript development where the focus has been on the basics and fundamental concepts of variable and access types. Hope you enjoyed and stay tuned for the next post which will be focusing on variable scope.

No comments:

Post a Comment