zoukankan      html  css  js  c++  java
  • Boolean类型Number类型String类型(一)

    //1、Boolean类型:是布尔值对应的引用类型
    //和布尔值有点区别,typeof()返回的是Object
    console.log("Boolean类型");
    var falseObject = new Boolean(false);
    var falseValue = false;
    
    console.log(typeof(falseObject));//object
    console.log(typeof(falseValue));//boolean
    console.log(falseObject instanceof Boolean);//true
    console.log(falseValue instanceof Boolean);//false
    
    var result = falseObject && true;
    console.log(result);//true  这里falseObject被当作一个对象,所以被转成了true
    result = falseValue && true;
    console.log(result);//false
    
    //2、Number类型:是与数字值对应的引用类型
    console.log("Number类型");
    /*
    *toFixed()方法:数值格式化为字符串
    *用法 Number.toFixed(num);
    *Number为要格式化为字符串的数值
    *num为参数:表示返回的字符串要几位小数,如果原本超过则四舍五入(不同浏览器可能会不同),如果原本就少于,则用0填充
    *num标准范围为0-20,不用浏览器可能会支持更多位
    */
    var num = 10;
    var num2 = 10.006;
    console.log(num.toFixed(2));//10.00
    console.log(num2.toFixed(2));//10.01
    
    /*
    *toExponential()方法:返回以指数表示法(也称e表示法)表示的数值的字符串形式。
    *用法 Number.toExponential(num);
    *和toFixed()一样,num也表示输出结果中的小数位数
    */
    var num3 = 10;
    console.log(num3.toExponential(1));//1.0e+1
    
    //如果想要得到表示某个数值的最合适格式,就用toPrecision()方法
    //用法 Number.toPrecision(num) 根据Number来自动决定是使用toFixed()方法还是toExponential()方法
    
    //3、String类型:字符串的对象包装类型
    console.log("String类型");
    //字符属性length
    //字符方法:charAt()和charCodeAt()
    //string.charAt(num) 返回string字符串num位置的字符
    //string.和charCodeAt(num) 返回string字符串num位置的字符的字符编码
    var stringValue = "hello world!";
    console.log(stringValue.charAt(2));//l
    console.log(stringValue.charCodeAt(2));//108
    
    //字符串操作方法:concat(),slice(),substr(),substring() 均不会改变原来的字符串,都是返回一个新的字符串
    //concat()方法:将一个或多个字符串拼接起来,返回拼接得到的新字符串(用的很少,一般直接用加号"+"操作符)
    console.log("concat()方法");
    var stringvalue2 = "hello ";
    var result2 = stringvalue2.concat("world!");
    console.log(result2);//hello world!
    
    /*
    *slice()方法:可以传递两个参数,第二个参数可省略
    *用法 string.slice(start, end); 返回start位置到end位置的字符串,不包括end位置的字符[start, end),end可省略,省略则返回到字符串末尾的字符串
    *注意,slice的参数可为负数,当为负数的时候,则把参数和字符串的长度相加,得到参数所表示的位置
    *
    *substring()方法:可以传递两个参数,第二个可省略
    *用法 string.substring(start, end);返回start位置到end位置的字符串,不包括end位置的字符[start, end),end可省略,省略则返回到字符串末尾的字符串
    *注意:参数不可为负数,如果为负数,则把所有负值参数都转换为0
    *
    *substr()方法:可以传递两个参数,第二个参数可省略
    *用法 string.substr(start, length); 返回start位置开始length个长度的字符串,如果第二个省略,则返回到字符串末尾的字符串
    *注意:substr()的两个参数,第一个为负数的时候,则计算为和字符串长度相加得到的值,第二个为负数的时候,则直接转换为0
    */
    console.log("slice()方法,substring()方法,substr()方法");
    var stringValue3 = "hello world";
    console.log(stringValue3.slice(-3));//rld            相当于stringValue3.slice(11+(-3));
    console.log(stringValue3.substring(-3));//hello world        相当于stringValue3.substring(0);
    console.log(stringValue3.substr(-3));//rld            相当于stringValue3.substr(8);
    console.log(stringValue3.slice(3, -4));//lo w       相当于stringValue3.slice(3, 7);
    console.log(stringValue3.substring(3, -4));//hel     相当于stringValue3.substring(3, 0);  可理解为stringValue3.substring(0, 3)
    console.log(stringValue3.substr(3, -4));//""         相当于stringValue3.substr(3, 0);
  • 相关阅读:
    python 安装 reportlab 报错 “ImportError: No module named reportlab.lib”
    人声和乐器的频谱范围
    C语言中指针中的值赋值给数组
    快速熟悉Matlab
    Ubuntu 16.04 下octave的使用入门
    IP达人启示录
    python常用的十进制、16进制之间的转换
    GNU 下命令objcopy 用法
    python zeros用法实例
    Debian/Ubuntu清理硬盘空间的8个技巧
  • 原文地址:https://www.cnblogs.com/qiangspecial/p/3130111.html
Copyright © 2011-2022 走看看