How can I detect if a variable is a string?
-
1You might want to use underscore.js, it has methods for this built in http://documentcloud.github.com/underscore/#isString - in case of isString it uses the method mentioned by user113716 – Martin Wawrusch Mar 09 '12 at 15:08
-
2possible duplicate of [Check if a variable is a string](http://stackoverflow.com/questions/4059147/check-if-a-variable-is-a-string) – Philipp Kyeck Feb 23 '14 at 22:06
-
possible duplicate of [Check whether variable is number or string in javascript](http://stackoverflow.com/questions/1303646/check-whether-variable-is-number-or-string-in-javascript) – Jonathan Hall Aug 21 '15 at 20:12
9 Answers
This is the way specified in the ECMAScript spec to determine the internal [[Class]] property.
if( Object.prototype.toString.call(myvar) == '[object String]' ) {
// a string
}
From 8.6.2 Object Internal Properties and Methods:
The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).
For an example of how this is useful, consider this example:
var str = new String('some string');
alert( typeof str ); // "object"
alert( Object.prototype.toString.call(str) ); // "[object String]"
If you use typeof
, you get "object"
.
But if you use the method above, you get the correct result "[object String]"
.

- 318,772
- 63
- 451
- 440
-
10not sure why this is the accepted answer. objects are not strings. `new String()` results in an object containing a series of indexed properties, each with a value corresponding to the character at that position. Yes, it will get coerced into a string via `Object`'s `toString` method, but the fact remains that `new String('some string')` is not a string, in and of itself. `typeof someString === "string"` is the correct way to determine if a variable is a string. Whether a variable can be *turned into* a string is moot; every object that can trace its prototype chain to `Object` has `toString` – jackwanders Aug 17 '12 at 19:16
You can use typeof
to do it, but for a lot of things this is bad design.
if (typeof myVar == "string") {
alert("I'm a string!");
}

- 55,313
- 14
- 116
- 115
-
Depends on what you're trying to do. Typically you should know what type you're receiving or it shouldn't matter (ie, you'll do the same thing to it regardless of the type). – Daniel DiPaolo Feb 03 '11 at 21:33
I don't think we need to treat new String('some string')
as a string, because if we try:
new String('abc')==='abc'
It will return false!
But if we try:
String('abc')==='abc'
It will return true.

- 51
- 1
- 2
var str = new String("some string");
if(str.constructor.name === "String")
{
alert("I'm a string!");
}
or simply:
if(str.constructor === String)
{
alert("I'm a string!");
}

- 17,007
- 37
- 111
- 185

- 1,039
- 2
- 18
- 25
-
2I got an Reference Error `_ is not defined`. Could you specify whether it requires package / specific jQuery version? – Lizesh Shakya Sep 24 '21 at 03:40
-
I mentioned that "Using underscore". Underscore is a JS Library: https://underscorejs.org/#notes – Diego Cotini Sep 29 '21 at 17:22