Home  >  Article  >  Web Front-end  >  JavaScript implements mutual conversion of data types_javascript skills

JavaScript implements mutual conversion of data types_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:11:491631browse

All languages ​​have the ability to type conversion, and JavaScript is no exception. It also provides developers with a large number of type conversion access methods. This article will share with you the mutual conversion of data types in js. The specific content is as follows

字符串转换数字
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));

数字转换字符串
var a = 1;
a+''
String(a);
a.toFixed();
a.toLocaleString();
a.toPrecision();
a.toString();

数组转字符串
var arr = [1,2,3];
arr.toString();
arr+"";

数组转数字,只能有一位,否则NaN
var arr = [1];
++arr;
+arr;
arr--;
arr-0
arr.toString()-0

The data obtained by js is of string type by default. If you perform numerical operations, you must use parseInt to convert into numerical values.

html code:

Copy code The code is as follows:

44cf29b3f11747cc7c113e413b9cc887
6825e4600b103d563280ef687d71b3c4
16b28748ea4df4d9c2150843fecfba68

js code:

$("#archive").bind('click',function(){
    
  var page=$("input[name='page']").val();
  //要用parseInt进行数值的运算
  $("input[name='page']").val(parseInt(page)+1);  

});

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn