Checking string type in JavaScript: a complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

"Is this a string?" — a question that haunts every JavaScript developer



You're writing code, and suddenly you stop. The variable should contain a string, but what if null got in there? Or a number? Or, even worse, undefined?



Sound familiar? If so, you're not alone. The question "Check if a variable is a string in JavaScript" is one of the most popular on Stack Overflow with tens of thousands of views. And this is no coincidence: JavaScript is a dynamically typed language, and type-related errors happen even to experienced developers.



In this article, we'll break down all possible ways to check if a variable is a string. You'll learn:

  • Why typeof is your best friend (but not always)
  • When instanceof lets you down
  • How the "sniper" method Object.prototype.toString works
  • And what to do if you use libraries


Let's go! 🚀



Method #1: typeof — the classic



The simplest and fastest way is the typeof operator. It returns a string with the type name:



const name = "Alice";const age = 25;const isAdmin = true;

console.log(typeof name); // "string"console.log(typeof age); // "number"console.log(typeof isAdmin); // "boolean"


Checking for a string looks like this:



if (typeof variable === "string") {  console.log("Yes, it's a string!");}


Pros:

  • Fast performance
  • No imports required
  • Readable


Cons:

  • Does not distinguish between the primitive "string" and the object new String("string")
  • Returns "object" for null (a historical JavaScript bug)


By the way, about null — this is a well-known language bug that won't be fixed to avoid breaking old websites. So typeof null === "object" is normal.



What about new String()?



In JavaScript, there are two types of strings: primitive (regular) and object (created with new String()). In practice, you'll almost never use new String(), but it's good to know about it:



const primitiveString = "Hello";const objectString = new String("Hello");

console.log(typeof primitiveString); // "string"console.log(typeof objectString); // "object"


If someone (or something) in your code passes an object string, typeof will deceive you. But don't worry: this is rare in real projects.



Method #2: instanceof — for object strings



The instanceof operator checks whether an object is an instance of a specific class:



const str = new String("Test");console.log(str instanceof String); // true

const str2 = "Test";console.log(str2 instanceof String); // false — because it's a primitive


As you can see, instanceof does not work with primitive strings. Therefore, it's rarely used — only if you know for sure you're working with objects.



When might it be useful? If you're writing a library that accepts both primitives and objects. But for 99% of cases, typeof will suffice.



Method #3: Object.prototype.toString.call() — the universal soldier



This method is a true sniper. It works for any data type and never makes a mistake:



function isString(value) {  return Object.prototype.toString.call(value) === "[object String]";}

console.log(isString("Hello")); // trueconsole.log(isString(new String("Hello"))); // trueconsole.log(isString(123)); // falseconsole.log(isString(null)); // falseconsole.log(isString(undefined)); // false


Blogs

Book Recommendations