DEV Community

Seonyoung Chloe (she/they)
Seonyoung Chloe (she/they)

Posted on

31 : Why is JavaScript considered as a dynamic programming language?

Dynamic - because of what you see on paper when you analyze the code, is most likely not what you’re going to get when the program runs.

JavaScript allows you to declare (for example) variables whose value (and kind) will only be known at run-time based on conditions “on the ground”.

In contrast, static languages will not compile into runnable code unless all values (or value types) are known upfront.

Here’s a quick JavaScript example of what makes it a dynamic language:

var thisIsAString = 'I am a string';
thisIsAString = 1; // assign an integer to thisIsAString
console.log(thisIsAString); // logs 1 to the console

The variable ‘thisIsAString’ began its life as a string,
but then I switched it to an integer.

If you pull up the console on your browser
(hitting F12 should do it in most browers),
you’ll notice that the code runs just fine
even though I changed the variable’s type during program execution.

Being able to change the underlying type of a variable while the program is running without causing an error is what makes JavaScript a dynamic language.

This behavior comes with both benefits and drawbacks, so I would encourage you to keep learning more about programming languages in order to understand the differences!

Top comments (0)