📖 Scenario: Imagine you are building a simple calculator that can add numbers or join words. JavaScript lets you change the type of a variable anytime. This is called dynamic typing.
🎯 Goal: You will create a variable, change its type, and see how JavaScript handles it.
📋 What You'll Learn
Create a variable called value and assign a number to it
Create a variable called text and assign a string to it
Change the variable value to a string
Use a console.log to show the final values
💡 Why This Matters
🌍 Real World
Dynamic typing helps when you want to store different kinds of data in the same variable during a program.
💼 Career
Understanding dynamic typing is important for JavaScript developers to write flexible and bug-free code.
Progress0 / 4 steps
1
Create a variable with a number
Create a variable called value and assign the number 10 to it.
Javascript
Hint
Use let to create a variable and assign the number 10.
2
Create a variable with a string
Create a variable called text and assign the string 'hello' to it.
Javascript
Hint
Use let to create a variable and assign the string 'hello'.
3
Change the variable type
Change the variable value to the string '10'.
Javascript
Hint
Assign the string '10' to the variable value.
4
Display the final values
Use console.log to print the values of value and text.
Javascript
Hint
Use console.log(value); and console.log(text); to show the values.
Practice
(1/5)
1. What does dynamic typing in JavaScript mean?
easy
A. Variables must be declared with a fixed type
B. Variables can hold any type and change types anytime
C. JavaScript does not allow variables
D. Types are checked only at compile time
Solution
Step 1: Understand variable typing in JavaScript
JavaScript variables do not have fixed types and can hold any value type.
Step 2: Define dynamic typing
Dynamic typing means variables can change their type during program execution.
Final Answer:
Variables can hold any type and change types anytime -> Option B
Quick Check:
Dynamic typing = variables change types [OK]
Hint: Remember: JavaScript variables are flexible with types [OK]
Common Mistakes:
Thinking variables have fixed types like in Java
Confusing dynamic typing with static typing
Believing JavaScript checks types before running
2. Which of the following is the correct way to declare a variable that can hold any type in JavaScript?
JavaScript uses let, var, or const without type annotations.
Step 2: Check each option
let x = 5; uses let correctly. int x = 5; uses int which is not valid in JavaScript. var x: number = 5; uses TypeScript syntax. const x = 'hello'; is valid but declares a constant, not a variable that can change.
Final Answer:
let x = 5; -> Option A
Quick Check:
Use let/var/const without types [OK]
Hint: Use let or var without type annotations [OK]
Common Mistakes:
Using type keywords like int or number in plain JavaScript
Confusing const with let for variable declaration
Trying to declare types explicitly in JavaScript
3. What will be the output of this code?
let value = 10;
value = 'hello';
console.log(typeof value);
medium
A. "number"
B. "undefined"
C. "string"
D. TypeError
Solution
Step 1: Track variable type changes
Initially, value is a number 10, then reassigned to string 'hello'.
Step 2: Evaluate typeof operator
typeof value returns the current type, which is "string" after reassignment.
Final Answer:
"string" -> Option C
Quick Check:
typeof after reassignment = "string" [OK]
Hint: typeof shows current type after reassignment [OK]
Common Mistakes:
Assuming type stays as number after reassignment
Confusing typeof output with variable value
Expecting an error when changing types
4. Find the error in this code snippet:
let data = 5;
data = true;
console.log(data.toFixed(2));
medium
A. TypeError because boolean has no toFixed method
B. No error, outputs "5.00"
C. SyntaxError due to reassignment
D. ReferenceError for data variable
Solution
Step 1: Understand variable type changes
Variable data starts as number 5, then changes to boolean true.
Step 2: Check method call on boolean
toFixed is a method for numbers, not booleans, so calling it on a boolean causes a TypeError.
Final Answer:
TypeError because boolean has no toFixed method -> Option A
Quick Check:
Calling number method on boolean causes TypeError [OK]
Hint: Check method compatibility with current variable type [OK]
Common Mistakes:
Assuming toFixed works on booleans
Thinking reassignment causes syntax error
Ignoring runtime errors from wrong method calls
5. You want to write a function that accepts any type of input and returns its type as a string. Which code correctly uses dynamic typing to achieve this?
function getType(input) {
// Your code here
}
hard
A. return typeof input.toString();
B. return input.type;
C. return input.constructor;
D. return typeof input;
Solution
Step 1: Understand typeof operator
typeof returns the type of any JavaScript value as a string.
Step 2: Evaluate each option
return typeof input; correctly returns the type string. return input.type; tries to access a property that may not exist. return input.constructor; returns a function, not a string. return typeof input.toString(); returns "string" always because toString() returns a string.
Final Answer:
return typeof input; -> Option D
Quick Check:
typeof returns type string for any input [OK]
Hint: Use typeof to get type as string for any value [OK]
Common Mistakes:
Using .type property which may be undefined
Returning constructor function instead of string
Calling toString() then typeof, always returns "string"
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.