js数据类型的检查

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // JavaScript语法之数据类型的检测
        // JavaScript中提供了一种方法,可以检测变量中存储的数据,是什么数据类型
        // typeof() 

        // 布尔值类型都是 boolean
        // console.log( typeof( true ) );
        // console.log( typeof( false ) );

        // 数值类型都是 number
        // console.log( typeof( 100 ) );
        // console.log( typeof( 100.123 ) );
        // console.log( typeof( 2e3 ) );
        // console.log( typeof( NaN ) );

        // 字符串类型都是 string
        // console.log( typeof( '北京' ) );
        // console.log( typeof( "上海" ) );
        // console.log( typeof( `重庆` ) );

        // undefined 是 undefined
        // console.log( typeof( undefined ) );

        // 函数的结果是 function
        // console.log( typeof( function fun(){console.log(123)} ) );

        // null 数组array 对象object 是 object
        // null array 在 JavaScript 中 严格来说,是 object 对象中一种特殊的类型形式
        // console.log( typeof( null ) );

        // console.log( typeof( [1,2,3,4,5] ) );

        // console.log( typeof( {name:'张三'} ) );

        
        // typeof的第二种语法
        // 不推荐使用 typeof 空格 数据/对象
        // 如果程序内容过多,就容易造成代码冲突
        // 还是用小括号包起来,比较安全

        // console.log( typeof 100 );

        // typeof() 的执行 结果,永远是字符串类型
        // 有的时候,有些面试题会问

        // 内层   typeof(null)  检测 null 的数据数据类型,结果是 object
        // 外层   typeof() 实际上 是 检查 内层 typeof() 执行结果的 数据类型
        // 内层   typeof(null) 的执行结果是 object ,数据类型是 字符串类型
        // 因此   外层 typeof() 实际上是 检测 typeof('object')
        // 执行结果是  string
        // console.log(  typeof( typeof( null ) )  ) ;
        //                       结果是 'object'
        //            typeof(      'object'     )
        // 外层的执行结果是  string   

        // 总结:
        // typeof( typeof() )
        // 内层的 typeof() 结果不管是什么内容,数据类型永远是 字符串
        // 外层的 typeof() 结果永远是 string 

        // 黑色是字符串
        // 蓝色不同,是 数值 或者 布尔值 
        // 灰色 undefined 和 null

        // 字符串是黑色
        console.log('字符串');

        // 数值是蓝色
        console.log(100);
        console.log(100.123);
        console.log(NaN);

        // 布尔类型
        console.log(true);
        console.log(false);

        // undefined 和 null
        console.log(undefined);
        console.log(null);

        // 同时输出多种数据类型
        // 字符串是红色
        console.log(true,100,'北京',undefined,null);









    </script>
</body>
</html>
右侧打赏一下 代码改变世界一块二块也是爱
原文地址:https://www.cnblogs.com/ht955/p/14011514.html