Yes,
JavaScript is dynamically typed language. In dynamically typed languages, you
no need to specify the data type to a variable, type of the variable is
determined at run time. Python, JavaScript, Julia are the examples of
dynamically typed languages.
HelloWorld.js
var a = "Hello Wolrd"; console.log("Value of a is " + a); a = 10; console.log("Value of a is " + a); a = true; console.log("Value of a is " + a); a = undefined; console.log("Value of a is " + a); a = {"name": "Krishna"}; console.log("Value of a is " + a);
Output
Value of a is Hello Wolrd Value of a is 10 Value of a is true Value of a is undefined Value of a is [object Object]
As
you see above snippet, I defined a variable ‘a’ like below.
var
a = "Hello Wolrd";
Later
I assigned a to the value 10.
a
= 10;
Later
I assigned a to true, undefined, and an object {"name":
"Krishna"}. This kind of redefinitions are possible in dynamically
typed languages, but not on statically typed languages.
Type conversion
If
an expression involves a string and number value with + operator, then
JavaScript converts the number values to strings.
var
x = "Hello" + 5; //Hello5
console.log(x)
If
the expression contains other operators (other than +), then number values are
not converted to strings.
HelloWorld.js
var x = "Hello" - 5; //NaN var y = "55"-5; //50 var z = "55"+5; //555 console.log(x); console.log(y); console.log(z);
If
you would like to know more differences between static and dynamically typed
languages, I would recommend you to go through my below post.
No comments:
Post a Comment