Variable Types
String Variables
Coding Tutorials
Programming Guide
Debugging Tips

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.

Browse interview questions

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:

javascript
1const value = "hello";
2
3if (typeof value === "string") {
4  console.log("primitive string");
5}

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:

javascript
1const primitive = "hello";
2const wrapped = new String("hello");
3
4console.log(typeof primitive); // "string"
5console.log(typeof wrapped);   // "object"
6console.log(wrapped instanceof String); // true

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:

python
1value = "hello"
2
3if isinstance(value, str):
4    print("text string")

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:

python
1value = b"hello"
2
3print(isinstance(value, str))   # False
4print(isinstance(value, bytes)) # True

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.

java
1Object value = "hello";
2
3if (value instanceof String) {
4    System.out.println("value is a String");
5}

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:

python
def requires_text(value: str) -> None:
    if not isinstance(value, str):
        raise TypeError("expected text")

But if conversion is acceptable, the better design may be:

python
value = str(value)

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 String parameters over Object when 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) == str in Python when isinstance is 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 str versus bytes distinction 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 String when 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions