Check type of varaiable in javascript
In javascript, you can get the type of variable in javascript with this function:
typeof(var)
example:
var a = 1;
>> typeof(a)
- return number
var b = "hello";
>> typeof(b)
- return string
var c = new Date();
>> typeof(c)
- return object
the sample of useful of this:
var man = {"name":"tom", "age" : 12};
if you want to display it according their type in table.
By String should align left, and number align right.
var html_table='<table><tr>';
for (var each in man){
html_table+='<td>'+each+'</td><td align="'
if (typeof(man[each])=='number') html_table+='right';
else if (typeof(man[each])=='string') html_table+='left';
html_table += '">'+man[each]+'</td>';
};
html_table+='</tr></table>';
after run, you will see the result like this
name | tom |
age | 12 |
No comments:
Post a Comment