javascript笔记

1.数值转换
    NaN    特殊的非数字值
2.字符串的使用
    msg = "Hello,"+"world";
    msg.length;
    last_char = msg.charAt(s.length-1);
    sub = msg.substring(1,4);
    i = msg.indexOf('a');
3.把数字转为字符串
    var n = 100;
    var s =  n+" bottles of beer on the wall.";
    (1)技巧,把数字转为字符串
     var n_as_string = n + "";
    (2)使用String()函数;
     var string_value = String(number);
    (3利用toString()方法;
     string_value = number.toString();
     里面可以传参数,2,8,16代表为进制,如
     var n = 17;
     binary_string = n.toString(2);  //value is "10001"
     octal_string = "0"+n.toString(8);  //value is "021"
     hex_string = "0x"+n.toString(16);  //value is "0x11"
     (4)toFixed()方法把一个数字转换为字符串
      var n = 123456.789;
      n.toFixed(0);    //1234567
      n.toFixed(2);    //123456.79
      n.toExponential(1);  //1.2e+5
4.把字符串转换为数字
      var product = "21"*"2"   ; //value is number 42
      (1) var number = Number(string_value);    //它不允许出现任何非空字符
      (2) parseInt,parseFloat
       parseInt("3 blind mice"); // value is 3
       parseFloat("3.14 meters");   // value is 3.14
       parseInt("eleven");  //value is NaN
       parseFloat("$72.14");                                                            

你可能感兴趣的:(JavaScript)