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

JavaScript implements mutual conversion of data types_javascript skills

WBOY
Release: 2016-05-16 15:11:49
Original
1666 people have browsed it

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
Copy after login

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:




js code:

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

});

Copy after login

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template