一、变量数据类型
Number
Sting
Boolean
Undefined
Object(对象属性)
Function(函数类型)
二、变量数字运算符
如果是纯数字,只能做减法、乘法、除法。不能做加法
三、变量类型转换
- 手动类型转换
Number()可以转为数字类型,前提是变量是纯数字的类型
parseInt()也可以将数字转换为整数类型,前提是里面有数字可以提取。
parseFloat()可以将数字转换为浮点数,前提是可以提取数字
- 自动类型转换
四、NaN何时出现
概念:Not A Number
在出现无法计算结果的时候,出现
NaN是number类型
NaN == NaN结果是false,不相等
五、进制转换
十进制转换其他进制 num.toString(16)//转换为16进制
其他转换为十进制 parseInt(“1010101”,”2”)二进制转换为十进制
六、关系运算符
===比较变量时,类型数值必须都相同
==比较变量,会自动转换类型
七、位运算
var a = 3&5;//按位与
//0000 0011 --->3
//0000 0101 --->5
//0000 0001
var b = 3|5;//按位或
//0000 0011 --->3
//0000 0101 --->5
//0000 0111
var c = 3^5;//按位异或 相异为一
//0000 0011 --->3
//0000 0101 --->5
//0000 0110
var d = 2<<3;// 左移位 2*2^3
//0000 0010
//0001 0000
八、 成绩分段显示小案例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<!-- <script src="index.js"></script> -->
</head>
<body>
<input type="text" id="score_input" value=""/><button onclick="cal()">提交</button>
</body>
<script>
function cal(){
var score = score_input.value;
if(score >= 90) {
console.log("成绩为A");
}else if(score >= 80) {
console.log("成绩为B");
}else if(score >= 70) {
console.log("成绩为C");
}else if(score >= 60) {
console.log("成绩为D");
}else {
console.log("成绩不合格");
}
}
</script>
</html>
九、年份月份判断
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<label>年份:</label><input type="text" id="year_input" value=""/><br>
<label>月份:</label><input type="text" id="month_input" value=""/><br>
<button onclick="cal()">提交</button>
</body>
<script>
function cal(){
var month = Number(month_input.value);
var year = Number(year_input.value);
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:console.log(month+"月共有31天"); break;
case 2:if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
console.log(month+"月共有28天"); break;
}else{
console.log(month+"月共有29天"); break;
}
case 4:
case 6:
case 9:
case 11:console.log(month+"月共有30天");break;
}
}
</script>
</html>