JavaScript: ECMAScript + BOM +DOM
javascript 标识符命名规则:
1、只能是字母、数字、下划线、$
2、不能以数字开头
3、不能使用关键字和保留字
省略var 声明的变量是全局变量,但是不推荐这种方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
// 一次声明多个变量
var name='cyy',age=24,gender;
</script>
</body>
</html>
js基本数据类型:
number string boolean undefined null
ES6新增数据类型
object
typeof 变量或者typeof(变量) 检测变量类型
变量的类型为string,变量类型的结果有:string number boolean object function undefined
null 表示空对象指针,如果定义了变量未来需要放置空对象,可以先赋值为null
如果定义了变量未来需要放置字符串,可以先赋值为""
如果定义了变量未来需要放置数字,可以先赋值为0
undefined 派生自 null,因此undefined==null 结果为true
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
var name='cyy',age=24,gender,settings=null;
console.log(typeof settings);//object
console.log(typeof gender);//undefined
console.log(undefined == null);//true
</script>
</body>
</html>
数字类型 number
特殊的数字类型 NaN
1、NaN的任何相关操作,结果都是NaN
2、NaN与任何数值都不相等,包括NaN本身
isNaN() 检测是否是非数值
首先会将值尝试转为数值,如果失败,则显示非数值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
console.log(15-'a');//NaN
console.log(typeof(15-'a'));//number
console.log(isNaN(15-'a'));//true
console.log(isNaN('16'));//false 尝试将字符串16转为数值16,成功
</script>
</body>
</html>
把非数值转为数值
Number() 可以将任何类型转为数值
parseInt() 用于将字符串转为整数
parseFloat() 用于将字符串转为浮点型
区别:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
var topval='28.56px';
console.log(Number(topval));//NaN
console.log(parseInt(topval));//28
console.log(parseFloat(topval));//28.56
</script>
</body>
</html>
来源:https://www.cnblogs.com/chenyingying0/p/12257574.html