HOWTO · JavaScript

How to Check if a Variable Is String in JavaScript

Learn how to check whether a JavaScript value is a primitive string, when String objects behave differently, and what alternative checks can and cannot tell you.

On this page

Use typeof value === "string" to check whether a value is a primitive string. This is the usual choice for validation. Single quotes, double quotes, and backticks all create primitive strings; the quote style does not change the type.

Check primitive strings with typeof

The typeof operator returns the string "string" for a primitive string. Compare its result with that exact value. For numbers, booleans, null, and objects, the comparison is false. This works for local values, function arguments, and values received from other code.

Run this example:

const primitive = "hello";
const boxed = new String("hello");
const number = 42;

console.log(typeof primitive);
console.log(typeof boxed);
console.log(typeof number === "string");
string
object
false

The primitive produces "string"; the value created with new String("hello") produces "object"; and 42 fails the primitive-string comparison. Use the comparison typeof value === "string" when validating a value.

Decide whether to accept String objects

JavaScript also has String objects, sometimes called boxed strings. new String("hello") creates one, while String("hello") without new returns a primitive string. Prefer primitives: boxed values have object behavior that can surprise comparisons and APIs.

The typeof test accepts primitive strings and rejects String objects, because typeof new String("hello") is "object". That is usually the right behavior for input validation. If an API intentionally accepts both forms, decide that explicitly rather than weakening every string check.

value instanceof String checks whether String.prototype from the current realm appears in the value’s prototype chain. It returns false for primitive strings and can also return false for a genuine String object created in a different realm, such as another browser window or frame. It does not prove that an object has String-wrapper internal data.

Understand the limits of alternative checks

Object.prototype.toString.call(value) === "[object String]" can identify primitive strings and ordinary String objects, including many cross-realm objects. It is not a universal authenticity test: objects can customize the result with Symbol.toStringTag. Use this check only when that limitation fits the values your code handles.

String.prototype.isPrototypeOf(value) checks whether the current realm’s String.prototype occurs in an object’s prototype chain. It applies to objects, not primitives, and has a cross-realm limitation similar to instanceof. Neither this method nor instanceof String proves that the value is a genuine String wrapper or contains internal string data: for example, Object.create(String.prototype) can pass both prototype-chain checks. These checks answer different questions and are not interchangeable. For ordinary type validation, use typeof value === "string" and decide separately whether boxed objects belong in the accepted input.

FAQ

How do I check if a variable is a string in JavaScript?

Use typeof value === "string" to accept primitive strings. This is the standard choice for most application input. If you also accept boxed values, specify that policy separately; prototype-chain checks alone do not prove an object is a genuine String wrapper.

What is the difference between primitive strings and String objects?

A primitive string is a string value. new String("hello") creates an object that wraps one, so typeof reports "object". Prefer primitive strings unless an API specifically needs a String object.

Does instanceof String work with primitive strings?

No. instanceof checks whether an object inherits from the current realm’s String constructor. Primitive strings are not objects.

Which check should I choose?

Use typeof for primitive-string validation. Use instanceof String or String.prototype.isPrototypeOf() only when you need to check for a same-realm String prototype chain; neither proves genuine wrapper identity. Use Object.prototype.toString.call() when you need to recognize primitive and boxed strings, while accounting for Symbol.toStringTag customization. A genuine cross-realm wrapper may fail the same-realm prototype checks.