1. JavaScript中字符串定义时,单引号和双引号没有区别
[javascript] view plaincopy
var str1 = “abc”;
var str2 = ‘xyz’;
2. Converting to a String
1)toString()
[javascript] view plaincopy
var age = 11;
var ageAsString = age.toString(); //the string “11”
var found = true;
var foundAsString = found.toString(); //the string “true”
var num = 10;
alert(num.toString()); //”10”
alert(num.toString(2)); //”1010”
alert(num.toString(8)); //”12”
alert(num.toString(10)); //”10”
alert(num.toString(16)); //”a”
toString()不适用于值为undefined和null的数据。
2)String()
[javascript] view plaincopy
var value1 = 10;
var value2 = true;
var value3 = null;
var value4;
alert(String(value1)); //”10”
alert(String(value2)); //”true”
alert(String(value3)); //”null”
alert(String(value4)); //”undefined”
String()可用于值为undefined和null的数据:
值为undefined的数据:返回值为“undefined”;
值为null的数据:返回值为“null”。
站长网
阅读(1378) | 评论(0) | 转发(0) |