Bird
Raised Fist0
Javascriptprogramming~10 mins

Type checking using typeof in Javascript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check the type of variable value.

Javascript
console.log(typeof [1]);
Drag options to blanks, or click blank then click option'
Avalue
B"value"
Ctypeof value
Dtypeof
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the variable name inside quotes, which makes it a string literal.
Using typeof inside typeof like a function call.
2fill in blank
medium

Complete the code to check if input is a string.

Javascript
if (typeof input === [1]) {
  console.log("It's a string!");
}
Drag options to blanks, or click blank then click option'
Astring
B"string"
C'string'
D"String"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the type name without quotes, causing a ReferenceError.
Using uppercase letters in the type string.
3fill in blank
hard

Fix the error in the code to correctly check if num is a number.

Javascript
if (typeof num === [1]) {
  console.log("It's a number!");
}
Drag options to blanks, or click blank then click option'
A"number"
B== "number"
C=== "number"
D"Number"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single equals sign = instead of ===.
Using uppercase letters in the type string.
4fill in blank
hard

Fill both blanks to create an object with keys as variable names and values as their types.

Javascript
const types = {
  name: [1],
  age: [2]
};
Drag options to blanks, or click blank then click option'
Atypeof name
Btypeof age
C"string"
D"number"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the typeof expressions, making them strings.
Using the type strings directly instead of typeof expressions.
5fill in blank
hard

Fill all three blanks to create a function that returns true if val is a boolean.

Javascript
function isBoolean(val) {
  return typeof [1] [2] [3];
}
Drag options to blanks, or click blank then click option'
Aval
B===
C"boolean"
Dval === "boolean"
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment = instead of comparison ===.
Comparing to boolean without quotes.
Putting the whole expression inside quotes.

Practice

(1/5)
1. What does the typeof operator return when used on the value 42?
easy
A. "string"
B. "object"
C. "boolean"
D. "number"

Solution

  1. Step 1: Understand the value type

    The value 42 is a number in JavaScript.
  2. Step 2: Apply typeof operator

    Using typeof 42 returns the string "number" because 42 is a numeric value.
  3. Final Answer:

    "number" -> Option D
  4. Quick Check:

    typeof 42 = "number" [OK]
Hint: Remember: numbers return "number" with typeof [OK]
Common Mistakes:
  • Thinking numbers return "string"
  • Confusing number with object
  • Using typeof without parentheses
2. Which of the following is the correct syntax to check if variable x is a string using typeof?
easy
A. if (typeof x = 'string') {}
B. if (typeof x === 'string') {}
C. if (typeof(x) == string) {}
D. if (typeof x === string) {}

Solution

  1. Step 1: Check correct operator usage

    The strict equality operator is ===, so typeof x === 'string' is correct.
  2. Step 2: Check string quotes and parentheses

    The type name must be a string literal in quotes, and parentheses around x are optional.
  3. Final Answer:

    if (typeof x === 'string') {} -> Option B
  4. Quick Check:

    Use === 'string' with quotes [OK]
Hint: Use triple equals and quotes around type name [OK]
Common Mistakes:
  • Using single equals (=) instead of triple (===)
  • Missing quotes around 'string'
  • Using typeof without quotes for type
3. What will be the output of the following code?
console.log(typeof null);
medium
A. "null"
B. "undefined"
C. "object"
D. "boolean"

Solution

  1. Step 1: Understand the value null

    In JavaScript, null is a special value representing "no object".
  2. Step 2: Apply typeof to null

    Due to a historical bug, typeof null returns "object" instead of "null".
  3. Final Answer:

    "object" -> Option C
  4. Quick Check:

    typeof null = "object" [OK]
Hint: Remember: typeof null returns "object" (quirk) [OK]
Common Mistakes:
  • Expecting "null" as output
  • Confusing with "undefined"
  • Thinking null is a boolean
4. Find the error in this code snippet:
let val = 10;
if (typeof val = "number") {
console.log("It's a number");
}
medium
A. Using single equals (=) instead of comparison operator
B. Missing semicolon after let declaration
C. Incorrect quotes around "number"
D. typeof cannot be used on variables

Solution

  1. Step 1: Check the if condition syntax

    The condition uses = which is assignment, not comparison.
  2. Step 2: Correct operator for comparison

    Use === or == to compare the result of typeof val with "number".
  3. Final Answer:

    Using single equals (=) instead of comparison operator -> Option A
  4. Quick Check:

    Use === for comparison, not = [OK]
Hint: Use === for comparison, not = [OK]
Common Mistakes:
  • Using = instead of === in if conditions
  • Thinking typeof returns variable type directly
  • Ignoring quotes around type string
5. You want to write a function isStringOrNumber that returns true if the input is either a string or a number, and false otherwise. Which of the following implementations is correct?
hard
A. function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; }
B. function isStringOrNumber(value) { return typeof value === 'string' && typeof value === 'number'; }
C. function isStringOrNumber(value) { return typeof value == 'string' && typeof value == 'number'; }
D. function isStringOrNumber(value) { return typeof value = 'string' || typeof value = 'number'; }

Solution

  1. Step 1: Understand the requirement

    The function should return true if the value is a string OR a number.
  2. Step 2: Check logical operators and comparisons

    function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; } uses || (OR) and strict equality ===, which is correct.
  3. Step 3: Identify errors in other options

    function isStringOrNumber(value) { return typeof value === 'string' && typeof value === 'number'; } and the option using == uses AND &&, which requires both types simultaneously (impossible). function isStringOrNumber(value) { return typeof value = 'string' || typeof value = 'number'; } uses assignment = instead of comparison.
  4. Final Answer:

    function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; } -> Option A
  5. Quick Check:

    Use OR (||) and === for type checks [OK]
Hint: Use || for OR and === for type checks [OK]
Common Mistakes:
  • Using && instead of || for OR condition
  • Using = instead of === for comparison
  • Mixing == and === inconsistently
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