JavaScript Code


How is JavaScript dynamically typed?


Dynamically Typed

Look at the following code.

x = 5;
console.log(typeof(x));

x = "apple";
console.log(typeof(x));

x = [52, 41, 0, 98];
console.log(typeof(x));

x is a variable. In the first statement, x is assigned a number. In the second statement, x is assigned a string. In the third statement, x is assigned an array.

x, once assigned with a type of value, say number, can be reassigned with value of another type. x can be set with values of different types in a single scope, dynamically. Hence, dynamically typed language.