Javascript – Check if a variable is a string in JavaScript

javascriptstring

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

Best Solution

This is what works for me:

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