Check if a variable is a string in JavaScript

javascript typeof string
javascript string contains
javascript check if object
javascript check if array
javascript instanceof string
check if string python
check if value is string python
javascript find in string

How can I determine whether a variable is a string or something else in JavaScript?


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" );
alert(typeof booleanValue) // displays "boolean"
alert(typeof numericalValue) // displays "number"
alert(typeof stringValue) // displays "string"
alert(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.


  1. The Google JavaScript Style Guide says to never use primitive object wrappers.
  2. Douglas Crockford recommended that primitive object wrappers be deprecated.

Check if a variable is a string in JavaScript, You can use typeof operator: var booleanValue = true; var numericalValue = 354; var stringValue = "This is a String"; var stringObject = new String( "This is a  JavaScript | Check if a variable is a string 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.


This is what works for me:

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

JavaScript, JavaScript | Check if a variable is a string. Checking the type of a variable can be done by using typeof operator. It directly applies either to a variable name or to  Check if a variable is a string in JavaScript In this post, we will see how to check if a variable is a string in JavaScript. The variable can either be a primitive value or an instance of a standard built-in ECMAScript object.


Since 580+ people have voted for an incorrect answer, and 800+ have voted for a working but shotgun-style answer, I thought it might be worth redoing my answer in a simpler form that everybody can understand.

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

Or, inline (I have an UltiSnip setup for this):

Object.prototype.toString.call(myVar) === "[object String]"

FYI, Pablo Santa Cruz's answer is wrong, because typeof new String("string") is object

DRAX's answer is accurate and functional, and should be the correct answer (since Pablo Santa Cruz is most definitely incorrect, and I won't argue against the popular vote.)

However, this answer is also definitely correct, and actually the best answer (except, perhaps, for the suggestion of using lodash/underscore). disclaimer: I contributed to the lodash 4 codebase.

My original answer (which obviously flew right over a lot of heads) follows:

I transcoded this from underscore.js:

['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'].forEach( 
    function(name) { 
        window['is' + name] = function(obj) {
              return toString.call(obj) == '[object ' + name + ']';
    }; 
});

That will define isString, isNumber, etc.


In Node.js, this can be implemented as a module:

module.exports = [
  'Arguments', 
  'Function', 
  'String', 
  'Number', 
  'Date', 
  'RegExp'
].reduce( (obj, name) => {
  obj[ 'is' + name ] = x => toString.call(x) == '[object ' + name + ']';
  return obj;
}, {});

JavaScript String includes() Method, Check if a string includes "world": var str = "Hello The includes() method determines whether a string contains the characters of a specified string. This method  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


I recommend using the built-in functions from jQuery or lodash/Underscore. They're simpler to use and easier to read.

Either function will handle the case DRAX mentioned... that is, they both check if (A) the variable is a string literal or (B) it's an instance of the String object. In either case, these functions correctly identify the value as being a string.

lodash / Underscore.js

if(_.isString(myVar))
   //it's a string
else
   //it's something else

jQuery

if($.type(myVar) === "string")
   //it's a string
else
   //it's something else

See lodash Documentation for _.isString() for more details.

See jQuery Documentation for $.type() for more details.

typeof, To check for anything more than just the primitive data types and to know if something's actually a number, string, null, an array or a real  for checking if the variable is a string (even if you use var str = new String("foo"), str instanceof String would return true). As for checking if it's a number I would go for the native: isNaN ; function.


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

I saw that here:

http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/

How to better check data types in javascript, If a function takes a DOM element as a parameter, it can be a string, or a jQuery object, for example. Or why not both? With a small conditional check it's possible. If you want to check if a variable exists, that can only be done with try/catch, since typeof will treat an undeclared variable and a variable declared with the value of undefined as equivalent. But, to check if a variable is declared and is not undefined :


JavaScript: check if function parameter is a string or an object , Check if JavaScript variable contains an object. typeof does Check if a value is a string in JavaScript. For strings, we can use a typeof check. But in your code you can use a wrapper check for an empty string like: // considering the variable in which your string is saved is named str. if (str && str.length>0) { // Your code here which you want to run if the string is not empty. Using this you can also make sure that string is not undefined or null too.


JavaScript Data Type Check Cheatsheet · Code with Hugo, Check if a variable is a string - Check if function parameter is a string or an object. Functions can be made less strict more human by not limiting the parameter  typeof – If variable is a number, it will returns a string named "number". isNaN() – Stands for "is Not a Number", if variable is not a number, it return true, else return false. Using typeof operator : In this example, we have used typeof operator, and verifying the variable is number or not in javscript.


javascript tutorial - Check if a variable is a string, I'm currently working on a project where we store certain values as either a string or an object. Depending on the type of the value, we will  Storing a String in a Variable. Variables in JavaScript are named containers that store a value, using the keywords var, const or let. We can assign the value of a string to a named variable. const newString = "This is a string assigned to a variable."; Now that the newString variable contains our string, we can reference it and print it to the