Home > Article > Web Front-end > JavaScript implements mutual conversion of data types_javascript skills
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:
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.