JavaScript - How is JS Dynamically Typed ?
Last Updated :
15 Nov, 2024
Improve
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
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
let x = 5 + "5";
console.log(x);
Output
55
A function can receive any type of argument.
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.