This is what works for me:

if (typeof myVar === 'string' || myVar instanceof String)
// it's a string
else
// it's something else

// Test this approach:

let isString = value => typeof value === 'string' || value instanceof String;

let falseCases = [
  [ 'null', null ],
  [ 'undefined', undefined ],
  [ 'object', { a: 1, b: 2 } ],
  [ 'array', [ 1, 2, 3 ] ],
  [ 'number', 123 ],
  [ 'zero', 0 ],
  [ 'RegExp', new RegExp('hello') ],
  [ 'number with valueOf returning string', Object.assign(10, { valueOf: () => 'abc' }) ],
  [ 'object pretending to be string', { constructor: String } ]
];
let trueCases = [
  [ 'empty literal string', '' ],
  [ 'unicode string literal', String.fromCharCode(10000) ],
  [ 'empty boxed string', new String('') ],
  [ 'unicode boxed string', new String(String.fromCharCode(10000)) ],
  [ 'string with overwritten "constructor"', Object.assign('hi', { constructor: Array }) ],
  [ 'string with overwritten "toString"', Object.assign('hi', { toString: 123 }) ],
  [ 'string with overwritten "valueOf"', Object.assign('hi', { valueOf: 123 }) ],
  [ 'string with overwritten "constructor"', Object.assign('hi', { constructor: RegExp }) ],
  [ 'proxied string', new Proxy(new String('hello'), {}) ],
];

console.log('NEGATIVE TESTS:');
for (let [ name, val ] of falseCases) {
  console.log(`Test ${name}:\n  Expect: false\n  Got:    ${isString(val)}`); 
}

console.log('\nPOSITIVE TESTS:');
for (let [ name, val ] of trueCases) {
  console.log(`Test ${name}:\n  Expect: true\n  Got:    ${isString(val)}`); 
}

Answer from DRAX on Stack Overflow
Top answer
1 of 16
3022

This is what works for me:

if (typeof myVar === 'string' || myVar instanceof String)
// it's a string
else
// it's something else

// Test this approach:

let isString = value => typeof value === 'string' || value instanceof String;

let falseCases = [
  [ 'null', null ],
  [ 'undefined', undefined ],
  [ 'object', { a: 1, b: 2 } ],
  [ 'array', [ 1, 2, 3 ] ],
  [ 'number', 123 ],
  [ 'zero', 0 ],
  [ 'RegExp', new RegExp('hello') ],
  [ 'number with valueOf returning string', Object.assign(10, { valueOf: () => 'abc' }) ],
  [ 'object pretending to be string', { constructor: String } ]
];
let trueCases = [
  [ 'empty literal string', '' ],
  [ 'unicode string literal', String.fromCharCode(10000) ],
  [ 'empty boxed string', new String('') ],
  [ 'unicode boxed string', new String(String.fromCharCode(10000)) ],
  [ 'string with overwritten "constructor"', Object.assign('hi', { constructor: Array }) ],
  [ 'string with overwritten "toString"', Object.assign('hi', { toString: 123 }) ],
  [ 'string with overwritten "valueOf"', Object.assign('hi', { valueOf: 123 }) ],
  [ 'string with overwritten "constructor"', Object.assign('hi', { constructor: RegExp }) ],
  [ 'proxied string', new Proxy(new String('hello'), {}) ],
];

console.log('NEGATIVE TESTS:');
for (let [ name, val ] of falseCases) {
  console.log(`Test ${name}:\n  Expect: false\n  Got:    ${isString(val)}`); 
}

console.log('\nPOSITIVE TESTS:');
for (let [ name, val ] of trueCases) {
  console.log(`Test ${name}:\n  Expect: true\n  Got:    ${isString(val)}`); 
}

2 of 16
2444

You can use typeof operator:

var booleanValue = true;
var numericalValue = 354;
var stringValue = "This is a String";
var stringObject = new String("This is a String Object");
console.log(typeof booleanValue) // displays "boolean"
console.log(typeof numericalValue) // displays "number"
console.log(typeof stringValue) // displays "string"
console.log(typeof stringObject) // displays "object"

Example from this webpage. (Example was slightly modified though).

This won't work as expected in the case of strings created with new String(), but this is seldom used and recommended against[1][2]. See the other answers for how to handle these, if you so desire.

// Test this approach:

let isString = value => typeof value === 'string';

let falseCases = [
  [ 'null', null ],
  [ 'undefined', undefined ],
  [ 'object', { a: 1, b: 2 } ],
  [ 'array', [ 1, 2, 3 ] ],
  [ 'number', 123 ],
  [ 'zero', 0 ],
  [ 'RegExp', new RegExp('hello') ],
  [ 'number with valueOf returning string', Object.assign(10, { valueOf: () => 'abc' }) ],
  [ 'object pretending to be string', { constructor: String } ]
];
let trueCases = [
  [ 'empty literal string', '' ],
  [ 'unicode string literal', String.fromCharCode(10000) ],
  [ 'empty boxed string', new String('') ],
  [ 'unicode boxed string', new String(String.fromCharCode(10000)) ],
  [ 'string with overwritten "constructor"', Object.assign('hi', { constructor: Array }) ],
  [ 'string with overwritten "toString"', Object.assign('hi', { toString: 123 }) ],
  [ 'string with overwritten "valueOf"', Object.assign('hi', { valueOf: 123 }) ],
  [ 'string with overwritten "constructor"', Object.assign('hi', { constructor: RegExp }) ],
  [ 'proxied string', new Proxy(new String('hello'), {}) ],
];

console.log('NEGATIVE TESTS:');
for (let [ name, val ] of falseCases) {
  console.log(`Test ${name}:\n  Expect: false\n  Got:    ${isString(val)}`); 
}

console.log('\nPOSITIVE TESTS:');
for (let [ name, val ] of trueCases) {
  console.log(`Test ${name}:\n  Expect: true\n  Got:    ${isString(val)}`); 
}


  1. The Google JavaScript Style Guide says to never use primitive object wrappers.
  2. Douglas Crockford recommended that primitive object wrappers be deprecated.
🌐
Zipy
zipy.ai › blog › check-if-a-variable-is-a-string-in-javascript
check if a variable is a string in javascript
April 12, 2024 - The typeof operator is the most straightforward method to check if a variable is a string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › check-if-a-variable-is-a-string-using-javascript
Check if a variable is a string using JavaScript - GeeksforGeeks
August 20, 2024 - let boolValue = true; let numValue ... } else { num = "is not a string"; } console.log(bool); console.log(num); ... The instanceof operator in JavaScript is used to check the type of an object at run time....
🌐
Sentry
sentry.io › sentry answers › javascript › check if a variable is a string in javascript
Check if a variable is a string in JavaScript | Sentry
July 15, 2023 - We can test this with the following code: // Function to test if variable is a string function isString(variable) { return typeof variable === "string"; } // Variables of different types let myBool = false; let myNum = 12; let myArray = [1, ...
🌐
Stack Abuse
stackabuse.com › javascript-check-if-variable-is-a-string
JavaScript: Check if Variable Is a String
May 6, 2022 - We'll cover that situation in this article! First, we'll take a look at how to check if a particular variable is a string in JavaScript and then show you an alternative approach that uses the Lodash library. In JavaScript, the typeof operator is the most used method to check the type of any ...
🌐
DEV Community
dev.to › lavary › four-ways-to-check-if-a-variable-is-a-string-in-javascript-with-examples-3ilm
Four ways to check if a variable is a string in JavaScript (with examples) - DEV Community
May 10, 2023 - Well, there's one scenario you might want to use instanceof to check strings in JavaScript, and that's when you need to create a universal utility function that works with all string types - no matter how they are defined: function isString(value) { return typeof value === 'string' || value instanceof String } You can use constructor.name, or toString.call(obj) to achieve the same result. In this article, we have discussed four different approaches to checking if a variable is a string in JavaScript.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-check-if-variable-is-string
Check if a Variable is a String using JavaScript | bobbyhadz
Use the `typeof` operator to check if a variable is a string, e.g. `if (typeof variable === 'string')`.
🌐
AskJavaScript
askjavascript.com › home › how to check if a variable is a string in javascript [6 ways]
How to Check If a Variable is a String in JavaScript [6 Ways]
November 9, 2023 - const stringVariable = "Harry Potter ... and false for nonStringVariable. In JavaScript, the Object.prototype.toString.call() method is a more reliable way to determine the type of an object, especially when it comes to built-in ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-check-if-a-variable-is-a-string
JavaScript | Check if a variable is a string - GeeksforGeeks
April 27, 2020 - Checking the type of a variable can be done by using typeof operator. It directly applies either to a variable name or to a variable. ... Example-1:This Example checks if the variable boolValue and numValue is string.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-check-if-a-variable-is-a-string-in-javascript.php
How to Check If a Variable is a String in JavaScript
// Sample variable var myVar = ... a custom function to check whether a variable is a string or not. The custom isString() JavaScript function in the following example will return true if the variable is a string otherwise ...
🌐
Code Beautify
codebeautify.org › blog › how-to-check-if-javascript-variable-is-string
How to Check if Javascript Variable Is String
February 22, 2024 - The simplest way to check a variable’s type is by using the typeof operator. For strings, typeof returns “string.” · Another approach is to use the instanceof operator, which checks if an object is an instance of a particular class or ...
🌐
Futurestud.io
futurestud.io › tutorials › how-to-check-if-a-value-is-a-string-in-javascript-or-node-js
How to Check if a Value is a String in JavaScript or Node.js
November 5, 2020 - You can then compare the type against the value 'string': const input = 'Hello Marcus, I’m a string value' const isString = typeof input === 'string' // true · If you want to be sure that a given value is actually a string, you should add a check on the prototype.
🌐
Squash
squash.io › how-to-check-if-a-variable-is-a-string-in-javascript
How to Check If a Variable Is a String in Javascript
April 1, 2024 - One way to check if a variable is a string in Javascript is by using the typeof operator.
🌐
Delft Stack
delftstack.com › home › howto › typescript › typescript check if string
How to Check if a Variable Is a String in TypeScript | Delft Stack
February 12, 2024 - In the above example, we declare a variable myVariable and assign it the string value 'John Doe'. Following that, we use an if statement with the typeof operator to check if the data type of myVariable is equal to the string literal 'string'.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript check if string
How to Check if a Variable Is String in JavaScript | Delft Stack
March 11, 2025 - By using the typeof operator, we can check the type of each variable. The first console.log returns true because variable1 is indeed a string, while the second returns false since variable2 is not a string.
🌐
DEV Community
dev.to › melvin2016 › how-to-check-if-a-variable-is-a-string-in-javascript-5c4m
How to check if a variable is a string in JavaScript? - DEV Community
March 14, 2021 - Originally posted here! To check if a variable is a string, you can use the typeof operator followed... Tagged with javascript.
🌐
LabEx
labex.io › tutorials › javascript-value-is-string-28444
Mastering JavaScript Type Checking | LabEx
Here's an example code that checks if a given value is a string: const isString = (val) => typeof val === "string";
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to check if variable is object or string in javascript
How to Check If Variable Is Object or String in Javascript
December 18, 2023 - By invoking toString.call() on an object or a string, we obtain a string representation that includes its type enclosed within square brackets. This allows us to easily identify whether the variable is an object or a string. In this article, we explored various methods for checking if a variable is an object or a string in JavaScript.
🌐
Futurestud.io
futurestud.io › tutorials › check-if-a-value-is-a-string-in-javascript-and-node-js
Check if a Value is a String in JavaScript and Node.js
April 8, 2021 - * * @param {*} input * * @returns ... { return typeof input === 'string' && Object.prototype.toString.call(input) === '[object String]' } isString('Hello Marcus') // true isString(123) // false · I guess in 99% of all cases you’re totally fine just using the typeof input === 'string' check...