当前位置:
首页 > 网站开发 > JavaScript >
-
js一——js的变量类型
一、js有如下:1、string类型;2、number类型;3、boolean类型;4、null类型;5、undefined类型;6、Object类型;7、Array类型;8、Function类型;9、Symbol类型。共九种数据类型。js把数据类型分为“基本数据类型”和“引用数据类型”。其中6、7、8为“引用数据类型”,其余为“基本数据类型”。
二、各类型数据演示。
-
number(数值类型)。不区分整数和小数
var num = 123 //声明并给num赋值 console.log("num = ", num) //输出num的值 var type = typeof (num) //typeof()方法可以获取数据的类型 console.log("num的数据类型是:", type) //输出数据类型
-
string(字符串类型)。
var str = "string" console.log("str = ", str) var type = typeof (str) console.log("str的数据类型是:", type)
-
boolean(布尔类型)
var bl = true console.log("bl = ", bl) var type = typeof (bl) console.log("bl的数据类型是:", type)
-
null(空类型)
var bl = null console.log("bl = ", bl) var type = typeof (bl) 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(未定义类型)
var ud = undefined console.log("ud = ", ud) var type = typeof (ud) console.log("ud的数据类型是:", type)
-
Object(对象类型)。此类型内可包含所有数据类型。
var obj = { num : 123, //数值型 str : 'str', //字符串型 bl : false, //布尔型, arr : [1,2,3], //数组型 nu : null, //空类型 ud : undefined //未定义类型 } console.log("obj = ", obj) var type = typeof (obj) console.log("obj的数据类型是:", type)
-
Array(数组类型)此类型内可包含所有数据类型。
var arr = [{ num : 123, //数值型 str : 'str', //字符串型 bl : false, //布尔型, nu : null, //空类型 ud : undefined //未定义类型 }, 123,'str',true,null,undefined] console.log("arr = ", arr) var type = typeof (arr) console.log("arr的数据类型是:", type)
此处arr的类型显示object的原因类似typeof(null)。
初学js,不足之处请大家指正。
出处:https://www.cnblogs.com/BanTang-o8o/p/17606398.html
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比