JavaScript language basics 6: javascript language basics
In the previous chapter, we know that when adding a character to a value in JavaScript code, the value is converted to a character, that is, the value entered by the user is converted to a character.
Now we want to add 1 and 2 to this example:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1="1";var value2="2"var intValue=value1+value2;document.write(intValue);</SCRIPT></BODY></HTML>
Obviously, this result is not what we want. Now we change the code to the following:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1="1";var value2="2"var intValue=parseInt(value1)+parseInt(value2);document.write(intValue);</SCRIPT></BODY></HTML>
This result is what we want, 1 + 2 = 3
That is to say, we can convert the character type to the numeric type using parseInt.
If the input value contains a value and a string, can we get the expected value?
See the following code:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1="2.34abc";var intValue=parseInt(value1);document.write(intValue);</SCRIPT></BODY></HTML>
It is true that a value can be obtained, but 2 is not 2.34. This is because the parseInt () function does not parse valid values. the resolution is stopped at, so how can we get 2.34 now? See the following code:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1="2.34abc";var intValue=parseFloat(value1);document.write(intValue);</SCRIPT></BODY></HTML>
The Code shows that the parseFloat () function can be used to parse a floating point number, that is, the value we want to get,
Next let's look at a special case. If there is no valid value in the input string, what will be obtained by using these two functions? See the following code:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1="abc";var intValue=parseFloat(value1);document.write(intValue);var value2="daf";var floatValue=parseFloat(value2);document.write("\n"+floatValue);</SCRIPT></BODY></HTML>
The NaN value is a special value in JavaScript, indicating that it is not a value.
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/40892573 sentiment control _
Lyrical music
Kiss the rain
Basic javascript Language exercises
--Why are you ..
<Script type = "text/javascript">
Function reverser (num ){
Var n = (num + ""). split ("");
N. sort ();
Document. write (n );
}
// Test the Function
Reverser (783782932 );
</Script>