DEV Community

Cover image for #5) Is JavaScript a statically typed or Dynamically typed languagešŸ¤”
Mayank Yadav
Mayank Yadav

Posted on • Updated on

#5) Is JavaScript a statically typed or Dynamically typed languagešŸ¤”

Yes, JavaScript is a dynamically typed language.

šŸš€Static Typed Languages

image

In the above example,šŸ‘†
We declare a variable name of string type by adding a prefix 'String' means it's value will always be of a string type.

But as we are going to re-assign the value of the variable name to some other data type which was earlier string, it will throw an error message and this only happens in statically typed language like TypeScript.

šŸš€Dynamic Typed Language

image

In the above example,šŸ‘†
We assign a value to the variable 'x' without defining it's type.
So, the type of the variable is set dynamically depending on the value that we assigned to the variable.

In the next step, we re-assigned the value of the variable 'x' to a number type.
So, there will be no error because the type of the variable is set dynamically during code execution.


āœ”JavaScript is a dynamically typed language so you are free to re-assign value of any type by using either let or var.
image


Note:

āœ…It's totally the developers responsibility to take care of the variables type and on a safer side use const.

āœ…In dynamically typed language, the type of a variable is checked during run-time whereas...
In statically typed language, the type of a variable is checked during compile-time.


Latest comments (0)