Adam Halasz
Adam Halasz

Reputation: 58301

Javascript: How to check string type?

So I would like to know what's inside the string for example:

var str = "a"; // Letter
var str = "1"; // Number
var str = "["; // Special
var str = "@"; // Special
var str = "+"; // Special

Is there any pre defined javascript function for this? Otherwise I will make it with regex :)

Upvotes: 1

Views: 1435

Answers (3)

mavirroco
mavirroco

Reputation: 117

if(isNaN(string)){
   //yes is a string
}

Upvotes: 0

El Ronnoco
El Ronnoco

Reputation: 11922

if (/^[a-zA-Z]$/.test(str)){
    // letter
} else if (/^[0-9]$/.test(str)){
    // number
} else {
    // other
};

Of course this only matches one character so 'AA' would end up in the //other section.

Upvotes: 3

Oded
Oded

Reputation: 498972

They are all strings...

There isn't anything built in that will do what you want.

A regex may be a good solution, though you have not really provided enough information for one.

Upvotes: 2

Related Questions