内置对象的相互转化

一.Number转化为String: 

1.toString()

var age = 11;
var ageString = age.toString();
console.log(typeof age);//Number
console.log(typeof ageString);//String

2.obj + ""

var age = 11;
var ageString = age + "";
console.log(typeof age);//Number
console.log(typeof ageString);//String

3.String()

var age = 11;
var ageString = String(age);
console.log(typeof age);//Number
console.log(typeof ageString);//String

 

二.String转化为Number

1.parseFloat()或parseInt()

var age = "11";
var ageNumber = parseInt(age);
console.log(typeof age);//String
console.log(typeof ageNumber);//Number

2.obj*1

var age = "11";
var ageNumber = age*1;
console.log(typeof age);//String
console.log(typeof ageNumber);//Number

3.Number()

var age = "11";
var ageNumber = Number(age);
console.log(typeof age);//String
console.log(typeof ageNumber);//Number

 

三.字符串转化为数组

1.split()

var age = "1123";
var ageArr = age.split("");
console.log(typeof age);//String
console.log(typeof ageArr);//Object
console.log(ageArr);//["1", "1", "2", "3"]

 

四.数组转化为字符串

1.join()

var age = ["1", "1", "2", "3"];
var ageString = age.join("");
console.log(typeof age);//Object
console.log(typeof ageString);//String
console.log(ageString);//1123

2.toString()

var age = ["1", "1", "2", "3"];
var ageString = age.toString();
console.log(typeof age);//Object
console.log(typeof ageString);//String
console.log(ageString);//1,1,2,3

 

posted @ 2016-03-30 16:10  PLDaily  阅读(200)  评论(0编辑  收藏  举报