Concept Flow - Dynamic typing in JavaScript
Declare variable
Assign value of any type
Use variable
Change variable type by reassigning
↩Back to Use variable
JavaScript variables can hold any type and can change type anytime by assigning new values.
let x = 10; x = "hello"; x = true; console.log(x);
| Step | Code Line | Variable | Value | Type | Explanation |
|---|---|---|---|---|---|
| 1 | let x = 10; | x | 10 | number | Variable x declared and assigned number 10 |
| 2 | x = "hello"; | x | "hello" | string | x now holds a string value |
| 3 | x = true; | x | true | boolean | x now holds a boolean value |
| 4 | console.log(x); | Prints current value of x, which is true |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| x | undefined | 10 (number) | "hello" (string) | true (boolean) | true (boolean) |
Dynamic typing means variables can hold any type and change types anytime. Declare with let or var, assign any value. No errors when changing types. Use console.log to see current value and type. JavaScript figures out types at runtime.