We use typeof to find out what kind of value a variable holds. This helps us avoid mistakes when working with different types of data.
Type checking using typeof in Javascript
typeof variableNametypeof returns a string describing the type, like "number", "string", "boolean", "undefined", "object", or "function".
You can use it directly in conditions to decide what to do based on the type.
typeof 42 // returns "number"
typeof "hello" // returns "string"
typeof true // returns "boolean"
typeof undefinedVariable // returns "undefined"
This program shows how typeof tells us the type of different values stored in the same variable.
let value = 100; console.log(typeof value); // number value = "JavaScript"; console.log(typeof value); // string value = false; console.log(typeof value); // boolean value = undefined; console.log(typeof value); // undefined value = { name: "Alice" }; console.log(typeof value); // object value = function() {}; console.log(typeof value); // function
typeof null returns "object" which is a known quirk in JavaScript.
Arrays are also of type "object" when checked with typeof.
Use typeof when you want a quick check of simple types.
typeof helps you find out the type of a value.
It returns a string like "number", "string", "boolean", "undefined", "object", or "function".
Use it to avoid errors by checking types before working with values.
Practice
typeof operator return when used on the value 42?Solution
Step 1: Understand the value type
The value42is a number in JavaScript.Step 2: Apply
Usingtypeofoperatortypeof 42returns the string "number" because 42 is a numeric value.Final Answer:
"number" -> Option DQuick Check:
typeof 42= "number" [OK]
- Thinking numbers return "string"
- Confusing number with object
- Using typeof without parentheses
x is a string using typeof?Solution
Step 1: Check correct operator usage
The strict equality operator is===, sotypeof x === 'string'is correct.Step 2: Check string quotes and parentheses
The type name must be a string literal in quotes, and parentheses aroundxare optional.Final Answer:
if (typeof x === 'string') {} -> Option BQuick Check:
Use=== 'string'with quotes [OK]
- Using single equals (=) instead of triple (===)
- Missing quotes around 'string'
- Using typeof without quotes for type
console.log(typeof null);
Solution
Step 1: Understand the value
In JavaScript,nullnullis a special value representing "no object".Step 2: Apply
Due to a historical bug,typeoftonulltypeof nullreturns "object" instead of "null".Final Answer:
"object" -> Option CQuick Check:
typeof null= "object" [OK]
- Expecting "null" as output
- Confusing with "undefined"
- Thinking null is a boolean
let val = 10;
if (typeof val = "number") {
console.log("It's a number");
}
Solution
Step 1: Check the if condition syntax
The condition uses=which is assignment, not comparison.Step 2: Correct operator for comparison
Use===or==to compare the result oftypeof valwith "number".Final Answer:
Using single equals (=) instead of comparison operator -> Option AQuick Check:
Use===for comparison, not=[OK]
- Using = instead of === in if conditions
- Thinking typeof returns variable type directly
- Ignoring quotes around type string
isStringOrNumber that returns true if the input is either a string or a number, and false otherwise. Which of the following implementations is correct?Solution
Step 1: Understand the requirement
The function should return true if the value is a string OR a number.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.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.Final Answer:
function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; } -> Option AQuick Check:
Use OR (||) and === for type checks [OK]
- Using && instead of || for OR condition
- Using = instead of === for comparison
- Mixing == and === inconsistently
Ready to prove it?
Pass the test and this pattern moves unknown
