How can I check if a var is a string in JavaScript?
I’ve tried this and it doesn’t work…
var a_string = "Hello, I'm a string.";
if (a_string typeof 'string') {
// this is a string
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You were close:
On a related note: the above check won’t work if a string is created with
new String('hello')as the type will beObjectinstead. There are complicated solutions to work around this, but it’s better to just avoid creating strings that way, ever.