JavaScript language basics 6: javascript language basics

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.