检查变量数据类型和非数字值

这篇博客介绍了JavaScript中使用typeof操作符检测变量数据类型,包括number、string、boolean、undefined和null。还讨论了NaN的特性,并展示了如何使用isNaN函数判断一个值是否为非数字。示例代码演示了各种数据类型的检测和NaN的使用。
摘要由CSDN通过智能技术生成

<!--

        检查变量数据类型

           typeof

             语法:

                typeof 变量

                   =>返回变量的数据类型  string number boolean undefined null

     -->

 <script>
        var num = 100  //创建一个名为num的变量,赋值100
        console.log('num :', typeof num )

        var userName = '小明' //创建名为userName的变量,将小明赋值给它
        console.log('username: ', typeof userName)

        var a
        console.log('a :', typeof a);  // undefined 未定义数据类型

        var sex = true
        console.log('sex :', typeof sex); //boolean

        var person = null  
        console.log('person :', typeof person); // object

    </script>

<!--

        NaN  (not a number)

        number   100

                  NaN

       

        var n1 = 100  

         

        isNaN(变量)

          => true|false

          => 如果变量是非数字符返回true,否则返回false

     -->

<script>
        var n1 = 'hello' 
        console.log( isNaN(n1) )

    </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值