js五种基本数据类型:string, number, boolean, null, undefined
2024-09-01 10:45:17
/**
* 五种基本数据类型:string, number, boolean, null, undefined
*/
// undefined
// 声明变量foo,未声明变量bar
var foo;
console.log(`typeof foo: ${foo}`, `typeof bar: ${bar}`); // typeof foo: undefined typeof bar: undefined
if (foo === undefined) { // foo全等于undefined
console.log('foo全等于undefined');
} else {
console.log('foo不全等于undefined');
}
if (typeof bar === 'undefined') { // bar全等于undefined
console.log('bar全等于undefined');
} else {
console.log('bar不全等于undefined');
}
// typeof 返回字符串类型
console.log(`typeof (typeof foo): ${typeof (typeof foo)}`); // typeof (typeof foo): string
// 定义函数f()但没有函数体,默认为undefined
function f() { }
console.log(`typeof f(): ${typeof f()}`, `f() === undefined: ${f() === undefined}`); // typeof f(): undefined f() === undefined: true // null
console.log(`typeof Null: ${typeof Null}`); // typeof Null: undefined
console.log(`typeof null: ${typeof null}`); // typeof null: object // string
// 常用转义字符
// ---------------------------------------------------------------------------
// \n 换行
// \t 制表符
// \b 空格
// \r 回车
// \f 换页符
// \\ 反斜杠
// \' 单引号
// \" 双引号
// \0nnn 八进制代码 nnn 表示的字符(n 是 0 到 7 中的一个八进制数字)
// \xnn 十六进制代码 nn 表示的字符(n 是 0 到 F 中的一个十六进制数字)
// \unnnn 十六进制代码 nnnn 表示的 Unicode 字符(n 是 0 到 F 中的一个十六进制数字)
// ---------------------------------------------------------------------------
var foo = 'hello';
console.log(`typeof foo: ${typeof foo}`); // typeof foo: string // boolean
var right = true,
wrong = false;
console.log(`typeof right: ${typeof right}, typeof wrong: ${typeof wrong}`); // typeof right: boolean, typeof wrong: boolean // number
// 整型
var foo = 100;
console.log(`typeof foo: ${typeof foo}`); // typeof foo: number
// 八进制
var foo = 017;
console.log(foo); //
// 十六进制
var foo = 0xAB;
console.log(foo) //
// 浮点数
var foo = 23.01;
console.log(foo); // 23.01
// 最大值
console.log(`Number.MAX_VALUE = ${Number.MAX_VALUE}`); // Number.MAX_VALUE = 1.7976931348623157e+308
// 最小值
console.log(`Number.MIN_VALUE = ${Number.MIN_VALUE}`); // Number.MIN_VALUE = 5e-324
// 正无穷大
console.log(`Number.POSITIVE_INFINITY = ${Number.POSITIVE_INFINITY}`); // Number.POSITIVE_INFINITY = Infinity
// 负无穷大
console.log(`Number.NEGATIVE_INFINITY = ${Number.NEGATIVE_INFINITY}`); // Number.NEGATIVE_INFINITY = -Infinity
// isFinite 验证是有限数字
var foo = Number.POSITIVE_INFINITY,
bar = foo * 3;
if (isFinite(bar)) { // bar是无穷大数字
console.log('bar是有限数字');
} else {
console.log('bar是无穷大数字');
}
// NaN 不是一个数字,特殊数值,不可用于直接计算
var foo = 'hello';
if (isNaN(foo)) { // foo不是数字
console.log('foo不是数字');
} else {
console.log('foo是数字');
}
最新文章
- socket.io简单入门(一.实现简单的图表推送)
- JavaScript Json对象和Json对象字符串的关系 jsonObj<;->;JsonString
- php 字符串和数字比较一些问题
- doPost()和doGet()
- 尚学堂Spring视频教程(五):Spring AOP
- 提高tomcat的并发能力
- Application 统计在线人数
- javascript调用oc的方法
- IOS 学习笔记 2015-03-22 OC-API-日期
- UIView类绘图出现错误提示
- Javascript基础 - js中曾经忽略的知识点
- python 之走坑的道路
- 中文代码示例之Vuejs入门教程(一)
- 【js】js中判断对数是否为空
- 【RL-TCPnet网络教程】第16章 UDP用户数据报协议基础知识
- DAY18 常用模块(二)
- 20175325 MyCP (课下作业,必做)
- C#基础知识总结(五)
- ES6 基础
- AbatorForEclipse插件使用总结