• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
风拂晚柳
博客园    首页    新随笔    联系   管理    订阅  订阅

JavaScript--详解typeof的用法

   typeof定义

     typeof是一元运算符,用来返回操作参数的类型(不是值)

   检查一个变量是否存在,是否有值

     typeof在两种情况下会返回"undefined":

  •      一个变量没有被声明的时候,
  •      一个变量没有赋值的时候
<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
            function testTypeOf() {
                //返回boolean
                console.log(typeof(true));
                
                //返回number
                console.log(typeof(1.2));
                
                //返回string
                console.log(typeof("foo"));
                
                //返回object
                console.log(typeof(new String("foo")));
                console.log(typeof(new Date()));
                console.log(typeof(null));
                console.log(typeof(new Number(1.2)));
                console.log(typeof(new Boolean(true)));
                console.log(typeof(new Array(1, 2, 3)));
                console.log(typeof(new Error()));
                console.log(typeof([1,2,3]));
                
                console.log(typeof(/abc/g));
                console.log(typeof(new RegExp("meow")));
                console.log(typeof({}));
                console.log(typeof(new Object()));
                
                //返回function
                console.log(typeof(new Function("")));
                
                //返回undefined
                var str;
                console.log(typeof(str));
                console.log(typeof(strLen));
            }
        </script>
    </head>

    <body>
        <button onclick="testTypeOf()">点击</button>
    </body>

</html>

 

posted @ 2019-01-09 11:44  风拂晚柳  阅读(489)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3