What if your code could instantly know what kind of data it's dealing with and avoid silly mistakes?
Why Type checking using typeof in Javascript? - Purpose & Use Cases
Imagine you are building a web app that takes user input. You want to make sure the input is a number before doing math with it. Without type checking, you might try to add a number to a string by mistake.
Manually guessing or assuming the type can cause bugs that are hard to find. You might add text to numbers, or call functions on wrong types, causing errors or wrong results. This slows you down and frustrates users.
The typeof operator quickly tells you the type of any value. You can check if something is a number, string, or something else before using it. This prevents errors and makes your code smarter and safer.
let input = '5'; // Assume input is number let result = input + 10; // Oops, this concatenates strings
let input = '5'; if (typeof input === 'number') { let result = input + 10; } else { console.log('Input is not a number'); }
Using typeof lets your program understand and handle different data types correctly, avoiding bugs and unexpected behavior.
When building a calculator app, you can check if the user input is a number before performing calculations, ensuring the app works smoothly without crashes.
Manual type guessing leads to bugs and confusion.
typeof gives a simple way to check data types.
This helps write safer, more reliable JavaScript code.
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
