How to check if type of a variable is string?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Checking whether a value is a string is a language-specific operation. The exact syntax changes, but the deeper question is always the same: are you checking for text values, for string objects, or for something that can be converted to text? Those are not identical requirements.
JavaScript: Use typeof for Primitive Strings
In JavaScript, the usual check is:
That is the standard answer for normal string values. It works because JavaScript's primitive string type reports itself as "string".
Where people get confused is wrapper objects:
So if you specifically care about ordinary string values, typeof value === "string" is usually the right check.
Python: Use isinstance(value, str)
In Python 3, text is represented by str, so the common check is:
This is usually better than comparing type(value) == str because isinstance also works with subclasses of str.
You should also keep text and bytes separate:
That distinction matters in file I/O, networking, and encoding-related bugs.
Java: Use instanceof
In Java, the check is usually only needed when you are holding a value as Object or through a broad interface.
This is also null-safe because null instanceof String evaluates to false.
In strongly typed Java code, though, runtime checks often signal that your API may be too loosely typed. Sometimes the better fix is to improve the method signature rather than to inspect types at runtime.
Choose the Check That Matches the Real Requirement
A lot of bad type checks come from an unclear goal. Ask yourself which of these you mean:
- the value must already be a string
- the value may be a string-like object
- the value can be converted to text if necessary
For example, in Python this is a stricter requirement:
But if conversion is acceptable, the better design may be:
Those are different policies, and the type check should reflect that.
Avoid Overusing Runtime Type Checks
Checking for strings is sometimes necessary at boundaries such as deserialization, API input validation, or debugging. But inside core program logic, too many runtime type checks can be a sign that the design should be more explicit.
Examples:
- in Java, prefer
Stringparameters overObjectwhen possible - in TypeScript or JavaScript, validate external data at the boundary
- in Python, document expected types and validate only where ambiguity matters
The safest code is usually the code that does not need to guess the type repeatedly.
Common Pitfalls
- Using
type(value) == strin Python whenisinstanceis more flexible. - Forgetting that
new String("x")in JavaScript is an object, not a primitive string. - Confusing "is a string" with "can be converted to a string".
- Adding runtime string checks in Java where stronger static typing would remove the need.
- Ignoring the
strversusbytesdistinction in Python 3.
Summary
- In JavaScript, use
typeof value === "string"for normal string values. - In Python, use
isinstance(value, str)for text strings. - In Java, use
instanceof Stringwhen runtime checking is genuinely needed. - Be clear whether you need an actual string or just something convertible to text.
- Frequent runtime type checks can indicate a design problem, not just a syntax problem.
Related reading
- How to check internet access on Android? InetAddress never times out
- How to check internet access on Android? InetAddress never times out
- How to check Kafka server status or details?
- How to check the output gradient by each layer in pytorch in my code?
- How to check whether Clickhouse server-settings is really applied?
- How to check whether my user data passing to EC2 instance is working
- How to check which zookeeper instance is the leader within an ensemble
- How to classify a failure detector?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.