• JaveScript简单数据类型(JS知识点归纳二)


    JS中的简单数据类型有五种 : 

        --> string

        --> number

        -->boolean

        --> null    

        -->undefined

    数据类型的检测 :typeof 

      语法:  typeof 数据;

             typeof ( 数据 ) ;

    1  typeof 100; //结果为number

        使用typeof获取的数据类型名是字符串类型

    1 var num = 100;
    2 var result = typeof num;//result此时保存的是变量str中数据的类型名
    3 console.log(typeof result);//"string"

    String类型

    1  使用成对的""   '',用于标识的引号是不会显示的,可以使用转义符() 显示

    2  强制转换

        数据.toString();

        

       var num = 100;
    
        console.log(num.toString());
        var result = num.toString();
        console.log(num);
        console.log(result);

        问题: 

        a,遇到数值类型的原值去进行toString等操作时,需要多加一个.符号

    var num -100;
    console.log(100..toString());//num.toString();

        b,undefined 和 null 不支持toString的功能

    undefined.toString();
    null.toString();

        String(数据);

    console.log(String(100));
    console.log(String(true));
    console.log(String(null));
    console.log(String(undefined));

    3  隐式转换

      使用其他数据类型和字符串类型进行+操作,就可以进行字符链接

    console.log("100abc" + 200);//"100abc200"

      通常为了不改变数据的原始内容,会使用空字符串进行隐式转换的操作

    var num = 100;
    console.log(num + "");

    Number类型

    1    整数类型
    2    小数类型
    3    不是数 NaN

    4    强制转换
        a,   Number(数据);


        b,  parseInt(数据);  

            转换为整数

            从左边开始,遇到不是数停止,首字符不是数,返回NaN


        c,  parseFloat(数据);  

            转换为小数 
    5    隐式转换

        +     前面不能有字符

        -  

        *

        /

        % 

    Boolean类型

    1    true

    2    false

          只有以下六个值是false,其他的都是true;

           0   ""   NaN   null   undefined    false

    3    强制转换

          Boolean(数据);

    4    隐式转换

          !!数据;

    NULL类型

    1  null类型是简单数据类型,只有一个值null,用于表示对象初始化之前的一种状态。

      检测:使用typeof检测时结果为"object",结果不准确。

        NULL并不是一个Object,可以算是JS中的一个原始Bug,它是一个原始值。

            可以使用Object.prototype.toString.call(null)  进行检测

    Object.prototype.toString.call(null);//"[object Null]"

    undefined类型

      undefined类型是简单数据类型,只有一个值undefined,表示变量未定义内容的一种状态。

    出现的场景

      

      1)变量声明未赋值
      2)数组元素不存在
      3)函数形参没有值
      4)函数的返回值--默认值
      5)对象属性不存在

    null与undefined由于每个类型只有一个值,所以没有转换操作。

  • 相关阅读:
    asy for
    asy html !
    lib
    git clone 指定 version tag
    git tag
    git clone <url>--depth 、 git clone <url> --recursive
    xelatex CLI
    rsync
    curl options
    [转自]C语言offset_of宏和container_of宏
  • 原文地址:https://www.cnblogs.com/AmorR/p/8083039.html
Copyright © 2020-2023  润新知