JavaScript implements the conversion of data types

  • 2021-01-22 04:53:20
  • OfStack

All languages have the ability to convert data, and javascript is no exception. It also has a large number of methods for converting data types. In this article, we will share the details below


 string-to-number 
var a = '1';
console.log(+a);
console.log(a++);
console.log(-a+3);
console.log(parseInt(a));
console.log(parseFloat(a));
console.log(Number(a));

 Numeric Conversion String 
var a = 1;
a+''
String(a);
a.toFixed();
a.toLocaleString();
a.toPrecision();
a.toString();

 Array to string 
var arr = [1,2,3];
arr.toString();
arr+"";

 Array to number, can only have 1 A, NaN
var arr = [1];
++arr;
+arr;
arr--;
arr-0
arr.toString()-0

By default, js retrieves data in string type string. If you perform numeric operations, you must use parseInt to convert data to numeric values.

html code:


<div id="archive">
    <input type="hidden" name="page" value="1" />
</div>

js code:


$("#archive").bind('click',function(){
    
  var page=$("input[name='page']").val();
  // Want to use parseInt Do numerical calculations 
  $("input[name='page']").val(parseInt(page)+1);  

});

The above is all the content of this article, I hope to help you learn.


Related articles: