This is the 5th question of the front-end interview questions series.

Dynamically-typed languages are those (like JavaScript) where the interpreter assigns variables a type at runtime based on the variable’s value at the time. —MDN

JavaScript is a dynamically-typed language, Meaning data types of variables are determined by the value they hold at runtime and can change throughout the program as we assign different values to them.

Example 👇

let a = 12; // type of the a is number
console.log(a); // Output: 12

a = ""; // no errors
console.log(a); // Output:

a = { name: "Rohan", age: 14 };
console.log(a); // Output: { name: "Rohan", age: 14 }

If we talk about Typescript, it is a statically typed language, where the type of a variable is known at compile time. This means that before the source code is compiled, the type associated with each variable must be known.

let a: number = 23;

a = ""; // Error: Type 'string' is not assignable to type 'number'.

Whether we define the data type of the a variable explicitly or not, the type of variable will be number because of Type inference ( only works when the initial value clearly indicates the type) in Typescript. we can not assign string to the variable which has number type.

If you find this helpful please share with your network ✅.

Happy Coding 🙌 !