翼度科技»论坛 编程开发 JavaScript 查看内容

js一——js的变量类型

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
一、js有如下:1、string类型;2、number类型;3、boolean类型;4、null类型;5、undefined类型;6、Object类型;7、Array类型;8、Function类型;9、Symbol类型。共九种数据类型。js把数据类型分为“基本数据类型”和“引用数据类型”。其中6、7、8为“引用数据类型”,其余为“基本数据类型”。

 
二、各类型数据演示。


  • number(数值类型)。不区分整数和小数
    1. var num = 123  //声明并给num赋值
    2. console.log("num = ", num) //输出num的值
    3. var type = typeof (num)  //typeof()方法可以获取数据的类型
    4. console.log("num的数据类型是:", type)  //输出数据类型
    复制代码

     
  • string(字符串类型)。
    1. var str = "string"
    2. console.log("str = ", str)
    3. var type = typeof (str)
    4. console.log("str的数据类型是:", type)
    复制代码

     
  • boolean(布尔类型)
    1. var bl = true
    2. console.log("bl = ", bl)
    3. var type = typeof (bl)
    4. console.log("bl的数据类型是:", type)
    复制代码

     
  • null(空类型)
    1. var bl = null
    2. console.log("bl = ", bl)
    3. var type = typeof (bl)
    4. console.log("bl的数据类型是:", type)
    复制代码

     上述typeof(bl)为object的原因:js的数据类型底层存储在二进制时,如下表示:

    • (1) 000:对象,数据是对象的应用。
    • (2) 1:整型,数据是31位带符号整数。
    • (3) 010:双精度类型,数据是双精度数字。
    • (4) 100:字符串,数据是字符串。
    • (5) 110:布尔类型,数据是布尔值。
    null的二进制表示为全“0”,故,typeof()方法会误以为null也为object类型。
    参考链接:https://blog.csdn.net/qq_45806781/article/details/118437729

  • undefined(未定义类型)
    1. var ud = undefined
    2. console.log("ud = ", ud)
    3. var type = typeof (ud)
    4. console.log("ud的数据类型是:", type)
    复制代码

     
  • Object(对象类型)。此类型内可包含所有数据类型。
    1. var obj = {
    2.     num : 123, //数值型
    3.     str : 'str',  //字符串型
    4.     bl : false, //布尔型,
    5.     arr : [1,2,3], //数组型
    6.     nu : null, //空类型
    7.     ud : undefined //未定义类型
    8. }
    9. console.log("obj = ", obj)
    10. var type = typeof (obj)
    11. console.log("obj的数据类型是:", type)
    复制代码

     
  • Array(数组类型)此类型内可包含所有数据类型。
    1. var arr = [{
    2.     num : 123, //数值型
    3.     str : 'str',  //字符串型
    4.     bl : false, //布尔型,
    5.    
    6.     nu : null, //空类型
    7.     ud : undefined //未定义类型
    8.     },
    9.     123,'str',true,null,undefined]
    10. console.log("arr = ", arr)
    11. var type = typeof (arr)
    12. console.log("arr的数据类型是:", type)
    复制代码

     此处arr的类型显示object的原因类似typeof(null)。
 
初学js,不足之处请大家指正。


来源:https://www.cnblogs.com/BanTang-o8o/p/17606398.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具