Bird
Raised Fist0
Javascriptprogramming~20 mins

Dynamic typing in JavaScript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JavaScript Dynamic Typing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code with dynamic typing?
Consider the following JavaScript code. What will be printed to the console?
Javascript
let x = '5';
x = x + 3;
console.log(x);
Aundefined
B8
CTypeError
D"53"
Attempts:
2 left
💡 Hint
Remember that + operator with a string converts the other operand to string.
❓ Predict Output
intermediate
2:00remaining
What is the type of variable after reassignment?
What will be the output of the following code?
Javascript
let data = 10;
data = 'hello';
console.log(typeof data);
A"number"
B"undefined"
C"string"
D"object"
Attempts:
2 left
💡 Hint
Variables in JavaScript can hold any type and can be reassigned.
❓ Predict Output
advanced
2:00remaining
What is the output of this dynamic type coercion?
What will this code print to the console?
Javascript
console.log('5' - 3);
A2
B"53"
CNaN
DTypeError
Attempts:
2 left
💡 Hint
The - operator converts strings to numbers if possible.
❓ Predict Output
advanced
2:00remaining
What error does this code raise due to dynamic typing?
What happens when this code runs?
Javascript
let a = null;
console.log(a.toUpperCase());
ATypeError
Bnull
Cundefined
DSyntaxError
Attempts:
2 left
💡 Hint
null has no methods and cannot be used like a string.
🧠 Conceptual
expert
3:00remaining
Which option shows the correct dynamic type change and output?
Which code snippet will output the number 10 after dynamic typing changes?
Alet val = '5'; val = val - '5'; console.log(val);
Blet val = '5'; val = +val + 5; console.log(val);
Clet val = 5; val = val + '5'; console.log(val);
Dlet val = '5'; val = val + 5; console.log(val);
Attempts:
2 left
💡 Hint
The unary + converts string to number before addition.

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

  1. Step 1: Understand variable typing in JavaScript

    JavaScript variables do not have fixed types and can hold any value type.
  2. Step 2: Define dynamic typing

    Dynamic typing means variables can change their type during program execution.
  3. Final Answer:

    Variables can hold any type and change types anytime -> Option B
  4. 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?
easy
A. let x = 5;
B. int x = 5;
C. var x: number = 5;
D. const x = 'hello';

Solution

  1. Step 1: Identify JavaScript variable declaration syntax

    JavaScript uses let, var, or const without type annotations.
  2. 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.
  3. Final Answer:

    let x = 5; -> Option A
  4. 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

  1. Step 1: Track variable type changes

    Initially, value is a number 10, then reassigned to string 'hello'.
  2. Step 2: Evaluate typeof operator

    typeof value returns the current type, which is "string" after reassignment.
  3. Final Answer:

    "string" -> Option C
  4. 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

  1. Step 1: Understand variable type changes

    Variable data starts as number 5, then changes to boolean true.
  2. Step 2: Check method call on boolean

    toFixed is a method for numbers, not booleans, so calling it on a boolean causes a TypeError.
  3. Final Answer:

    TypeError because boolean has no toFixed method -> Option A
  4. 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

  1. Step 1: Understand typeof operator

    typeof returns the type of any JavaScript value as a string.
  2. 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.
  3. Final Answer:

    return typeof input; -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard