Question
How can I determine whether a variable is a string or another type in JavaScript? For example, I want to reliably check whether a value is a string before using string-specific methods on it.
Short Answer
By the end of this page, you will understand how JavaScript represents strings, how to test whether a value is a string, and which approaches are safest in real code. You will also see common mistakes, practical examples, and how developers usually handle type checks in applications.
Concept
In JavaScript, a string is a sequence of characters such as "hello" or 'abc'. Strings are one of JavaScript's primitive types.
The most common way to check whether a value is a string is with typeof:
typeof value === "string"
This works well for normal string values because JavaScript reports primitive strings as "string".
Why this matters
In real programs, values often come from:
- user input
- APIs
- form fields
- configuration files
- function arguments
Before calling string methods like .toLowerCase(), .trim(), or .includes(), it is often a good idea to confirm that the value is actually a string. Otherwise, your code may throw errors such as:
TypeError: value.trim is not a function
Primitive strings vs String objects
JavaScript has two related but different forms:
const a = "hello"; // primitive string
const b = new String("hello"); // String object
Most of the time, you should use primitive strings. new String() creates an object wrapper, which behaves differently and can cause confusion.
For this reason, typeof is usually the best check for everyday JavaScript:
typeof a === "string" // true
typeof b === "string" // false
If you need to accept both primitive strings and String objects, you can combine checks:
typeof value === "string" || value instanceof String
That said, in beginner and production code, avoiding new String() is usually the better choice.
Mental Model
Think of JavaScript values as items in labeled boxes.
- A primitive string is a simple note with text written on it.
- A
Stringobject is a note placed inside a container.
If you ask JavaScript typeof, it tells you the label on the outside:
- primitive string →
"string" Stringobject →"object"
So if you only want normal text values, typeof is enough. If you also want to allow wrapped text inside a container, you need an extra check.
Syntax and Examples
The most common syntax is:
typeof value === "string"
Example 1: Basic string check
const name = "Ada";
if (typeof name === "string") {
console.log("name is a string");
} else {
console.log("name is not a string");
}
This prints:
name is a string
Example 2: Non-string value
const age = 42;
console.log(typeof age === "string");
Output:
false
Example 3: Accept primitive strings and String objects
() {
value === || value ;
}
.(());
.(( ()));
.(());
.(());
Step by Step Execution
Consider this example:
function describeValue(value) {
if (typeof value === "string") {
console.log("String: " + value.toUpperCase());
} else {
console.log("Not a string");
}
}
describeValue("cat");
describeValue(10);
Step by step:
-
The function
describeValueis defined with one parameter:value. -
describeValue("cat")is called. -
Inside the function, JavaScript evaluates:
typeof value === "string" -
Since
valueis"cat",typeof valuebecomes .
Real World Use Cases
Checking whether a value is a string is useful in many real situations.
Form handling
function normalizeUsername(username) {
if (typeof username !== "string") return "";
return username.trim().toLowerCase();
}
This protects your code if a bad value is passed in.
API response validation
function readMessage(data) {
if (typeof data.message === "string") {
return data.message;
}
return "No message available";
}
APIs do not always return the shape you expect.
Command-line scripts
function processArgument(arg) {
if (typeof arg === "string") {
console.(, arg.);
}
}
Real Codebase Usage
In real codebases, developers often use string checks as part of validation and guard clauses.
Guard clauses
A guard clause exits early when the input is invalid.
function formatTitle(title) {
if (typeof title !== "string") {
return "Untitled";
}
return title.trim();
}
This keeps the rest of the function simpler.
Input validation
function createUser(data) {
if (typeof data.name !== "string") {
throw new Error("name must be a string");
}
return {
name: data.name.trim()
};
}
Filtering collections
const mixed = ["x", 1, , {}, ];
strings = mixed.( item === );
Common Mistakes
1. Using instanceof String by itself
This misses normal primitive strings.
console.log("hello" instanceof String); // false
console.log(new String("hello") instanceof String); // true
Why it happens:
"hello"is a primitive, not an object.
Better approach:
typeof value === "string"
2. Forgetting about new String()
const value = new String("hello");
console.log(typeof value); // "object"
If your code only checks , this object will not pass.
Comparisons
| Check | What it does | Works for primitive strings | Works for new String() | Recommended |
|---|---|---|---|---|
typeof value === "string" | Checks whether the value is a primitive string | Yes | No | Yes |
value instanceof String | Checks whether the value is a String object | No | Yes | Rarely |
| `typeof value === "string" | value instanceof String` | Accepts both forms | Yes | |
Object.prototype.toString.call(value) === "[object String]" | More explicit internal tag check |
Cheat Sheet
Quick check
typeof value === "string"
Accept both primitive strings and String objects
typeof value === "string" || value instanceof String
Common results
typeof "hello" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof null // "object" (historical quirk)
typeof undefined // "undefined"
typeof new String("hello") // "object"
Safe helper function
function isString(value) {
return value === ;
}
FAQ
How do I check if a variable is a string in JavaScript?
Use:
typeof value === "string"
This is the standard and most common solution.
Why does instanceof String return false for normal strings?
Because normal strings are primitive values, not String objects. instanceof only works with objects created from constructors.
Should I use new String() in JavaScript?
Usually no. It creates a wrapper object instead of a primitive string, which can lead to confusing behavior.
Can I safely call .trim() after checking with typeof?
Yes, if typeof value === "string" is true, then string methods like .trim() are safe to use.
How do I check for both primitive strings and String objects?
Use:
typeof value === "string" || value instanceof
Mini Project
Description
Build a small utility that safely formats user-provided text. This project demonstrates how to check whether a value is a string before using string methods such as trim() and toUpperCase(). This is useful in real applications where input may come from forms, APIs, or other untrusted sources.
Goal
Create a function that accepts any value and returns a cleaned uppercase string only when the input is actually a string.
Requirements
- Create a function named
formatInput. - If the input is not a string, return
"Invalid input". - If the input is a string, remove extra spaces with
trim(). - Convert valid string input to uppercase.
- Test the function with a string, a number, and
null.
Keep learning
Related questions
Adding Table Rows with jQuery: append(), Limits, and Best Practices
Learn how to add table rows in jQuery using append(), what elements are allowed in tables, and safer ways to build rows dynamically.
Bower vs npm: What’s the Difference in JavaScript Package Management?
Learn the plain difference between Bower and npm, how each manages packages, and why npm replaced Bower in most JavaScript projects.
Can `(a == 1 && a == 2 && a == 3)` Ever Be True in JavaScript?
Learn how JavaScript type coercion, loose equality, and custom object conversion can make `a == 1 && a == 2 && a == 3` true.