美文网首页
变量类型

变量类型

作者: 晴天3521 | 来源:发表于2018-11-06 13:59 被阅读0次

    变量类型

    今天我们来学习一下变量类型中的typeof运算符(从字面上来翻译就是找到一个东西的类型),我们常见的类型有number、string、boolean、undefined、function和object,接下来我们来分别学习一下。
    1.number类型

    var a = 12;
     alert(typeof a); //number
    
    number类型

    2.string类型

    a = 'asdfghjkl';
    alert(typeof a);
    
    string类型

    3.boolean类型

    a = true;
       alert(typeof a);
    
    boolean类型

    4.function类型

    a = function() {
                    alert('asdf');
                }
                alert(typeof a);
    
    function类型

    5.object类型

    a = document;
    alert(typeof a);
    
    object类型

    6.undefined类型

    alert(typeof b);
    

    或者:

    var b;
    alert(typeof b);
    
    undefined类型

    出现undefined情况有两种原因:
    1.真的没定义。
    2.虽然定义了,没有给东西。
    接下来我把所有的代码放在一起。

    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title>变量类型</title>
            <script>
                var a = 12;
                //          alert(typeof a); //number
                a = 'asdfghjkl';
                //          alert(typeof a); //string
                a = true;
                //          alert(typeof a); //boolean
                a = function() {
                    alert('asdf');
                }
                //          alert(typeof a);//function
                a = document;
                //          alert(typeof a);//object
                var b;
                alert(typeof b); //undefined
                //1.真的没定义
                //2.虽然定义了,没有给东西
            </script>
        </head>
    
        <body>
        </body>
    
    </html>
    

    因此,为了方便,在写代码的过程中一个变量应该只存放一种类型的数据。
    好了,今天的学习就到这里了。
    愿你三冬暖,愿你春不寒; 愿你天黑有灯,下雨有伞。


    小晴天.jpg

    相关文章

      网友评论

          本文标题:变量类型

          本文链接:https://www.haomeiwen.com/subject/bzzyxqtx.html