Js effect two plus sign (+) and basic data type conversion

Plus sign (+) of two effects:

1, when the number of values ​​are added on both sides, an addition operation is performed

2, when the character string plus side, the splicing is performed

Basic data type conversion:

1, the string values ​​into toString ()

    Format: numeric variable name .toString ()

2, the integer string into the parseInt ()

    Precautions: Only the first character string into digital if not a number will be reported NaN (Not a Number);

    Format: parseInt (string variable names)

3, the string into float pareFloat ()

    Format: parseFloat (value of variable names)

 

If the addition number string, the result will be a string

Code demonstrates:

. 1  <! DOCTYPE HTML > 
2  < HTML > 
. 3  < head >  
. 4  < Meta charset = "UTF-. 8" >  
. 5  < title > novice tutorial (runoob.com) </ title >  
. 6  </ head > 
. 7  < body > 
. 8  
9  < the p- > click the button to create and increase the string variable. </ P > 
10  < Button the onclick = "myFunction ()" > Click here <>
11 <p id="demo"></p>
12 </body>
13 </html>
14 <script>
15 function myFunction()
16 {
17     var x=5+5;
18     var y="5"+5;
19     var z="Hello"+5;
20     var demoP=document.getElementById("demo");
21     demoP.innerHTML=x + "<br>" + y + "<br>" + z;
22 }
23 </script>
View Code

Page display

 

Guess you like

Origin www.cnblogs.com/z617182272/p/11375373.html