Open In App

JavaScript - How is JS Dynamically Typed ?

Last Updated : 15 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Writing a data type (like we do in C/C++/Java) is not required

A variable name can hold any type of information

Mainly data type is stored with value (not with variable name) and is decided & checked at run time

JavaScript
let x = 42;   
console.log(x)

x = "hello";  
console.log(x)

x = [1, 2, 3]
console.log(x)

Output
42
hello
[ 1, 2, 3 ]

Type coercion happens automatically

JavaScript
let x = 5 + "5";
console.log(x);

Output
55

A function can receive any type of argument.

JavaScript
function print(x) {
  console.log(x);
}

print(42);        
print("hello");   
print({ key: "val" });

Output
42
hello
{ key: 'val' }

Note that dynamically typed languages are easier to program, but slower. TypeScript is a variation of JavaScript that is statically typed.




Next Article

Similar Reads