Dynamic Typing in Javascript

In the previous section, we have seen that there are seven primitive data types in javascript, but there is another fundamental thing to know about types. Which is the fact that javascript has a feature called Dynamic Typing. This article contains a lot of abstraction, so I would suggest you read slowly and carefully; skimming is not the best way for this article.

Dynamic Typing means that whenever we create a new variable, you do not have to manually define the data type of the value that it contains.

In many other programming languages, you actually have to define the datatype of a variable manually, but not in javascript. Instead, javascript automatically determines the data type of the value when it is stored into a variable. And this distinction between a value and variable is crucial in javascript because, in javascript, it's the value that has a type, not the variable.

So variables simply store values that has a type. Many people don't know about this detail or just don't care, but this is how it works.

Another important application of Dynamic Typing is that later in our code, we can assign a new value with a different data type to the same variable without any issue.
let x = 20; // Varible x is intially is of type "number"
let x = 'Hello' // Now Variable x is a type of "string"
Everything is taken care of by javascript.

And this can be of course be very useful but it can also be the source of some difficulty to find bugs which means errors in our code.

Dynamic Typing in Action

typeof is an operator used to find the type of value holding within a variable? Let's check it in action.

let hero = 'Naruto';
let myAge = 24;
let javascriptIsFun = true;

console.log(typeof hero); // string
console.log(typeof myAge); // number
console.log(typeof javascriptIsFun); // boolean

// Let's see what will happen if we change the type of data
// Dynamic Typing in action
javascriptIsFun = 'yes it is'; 
console.log(typeof javascriptIsFun); // string
Javascript will be executed from top to bottom.

In the above code example, initially, the variable javascriptIsFun contains the value true which is a boolean, so even the variable type is boolean. As javascript supports Dynamic Typing, we can change the value of the variable dynamically. This means that if the new value is of a different type, the type of the variable will also vary accordingly. This can also be seen when the value of javascriptIsFun became a string yes it is.

In javascript, the type of a variable is determined automatically according to the value it is holding.
// Let's create a variable year
let year;
console.log(year); // undefined
console.log(typeof year); // undefined

// Let's assign a value to the variable year
// year is a defined variable, so no need to use let
// we use let to create a variable
year = 2021; 
console.log(year); // 2021
console.log(typeof year); // number
Another example to show that type of a variable is dependent on value.

In the above code example, initially, the variable year was defined but doesn't hold any value; which means it is a defined variable with an undefined value. As the value is undefined the type of the variable is also undefined

I'm reiterating it again, undefined is different from not defined. In the above code example initially, the variable year is defined but it has no value; so it is undefined. Later as we assigned a value 2021 to the variable year, it now has a value and type.

// Let's create a variable year
let year; // variable year is defined, but its value is undefined
console.log(year) // undefined

year = 2021; // variable year is defined and its value is 2021
console.log(year) // 2021

console.log(month) // variable month is not defined

// we can both create and assign a value to a variable in one line
let month = 'October'; // now we defined the variable month
console.log(month); // October
console.log(typeof month) // string
If you don't comment the line 7, this will result in an error.
Loading comments...
You've successfully subscribed to Leewardslope
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.