Jump to content

isNaN();

- - - - -

  • Please log in to reply
2 replies to this topic

#1
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I have two variable, test1 and test2. The values in those variables are as a string because I use ""




<script language="JavaScript" type="text/javascript">

var test1="123";

var test2="123abc";


if(isNaN(test1)){

document.write(test1 +  " <== is Not_a_Number, It's a String<br>");}


else if(!isNaN(test1)){

document.write(test1  + " <== is a Number <br>");}



if(isNaN(test2)){

document.write(test2  +" <== is Not_a_Number, It's a String <br>");}


else if(!isNaN(test2)){

document.write(test2  +" <== is a Number <br>");}

</script>


the script tells me:
123 <== is a Number

123abc <== is Not_a_Number, It's a String 
but "123" is also script, not a number.

number will be if i use parseInt(test1) or Number(test1), If I want to know whether variale is a string or Number, what do I do?




Other question: what is diference between ParseInt() and Number() ? is Number() general becaouse I have also ParseFloat() function?

#2
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
If what you are asking is how to find the type of a variable, then what you need to do is use javascript typeof.

var x = 123;
var y = "123";

alert(typeof x); // number
alert(typeof y); // string

#3
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
thanks a lot :)
GNU/Linux Is the Best.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users