一、数据类型
逻辑运算
&& //两个都是真,结果为真
|| //一个为真,结果为真
! //真即假,假即真
比较运算符
= 赋值,不是比较运算符
== 等于(类型不一样,值一样,也会判断为true)
=== 绝对等于(类型一样,值一样,结果true)
严格模式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
前提:idea需要设置支持es6的语法
use strict 严格校验模式,预防JavaScript的随意性导致产生的一些问题
必须写在JavaScript的第一行
局部变量建议都使用let去定义
-->
<script>
'use strict'
//Uncaught ReferenceError: j is not defined
// j = 1;//全局变量
// var i = 1;
//局部变量
let i = 1;
</script>
</head>
<body>
</body>
</html>
三、字符串
模板字符串
let name = "zyy";
let msg = `hello ${name}`;
console.log(msg); //hello zyy
字符串长度
let str = "student";
console.log(str.length); // 7
大小写转换
//注意,这里是防范,不是属性 str = "student"
str.toUpperCase() //STUDENT
str.toLowerCase() //student
找字符的对应字符串中的下标
//str = "student"
console.log(str.indexOf('s')) //0
截取字符串
// [) str = "student"
console.log(str.substring(0)) //student
console.log(str.substring(1)) //tudent
console.log(str.substring(1,2)) //t
四、Iterable迭代器
遍历数组
let arr = [3,4,5];
for(let i of arr) {
console.log(i)
}
遍历Map
let map = new Map([['张三',100],['李四',90],['王五',80]]);
for (let i of map) {
console.log(i);
}
五、函数的定义和参数获取
1.绝对值函数
function abs(x) {
if (x >= 0) {
return x;
} else {
return -x;
}
}
一旦执行到return代表函数结束,返回结果!
如果没有执行return,函数执行完也会返回结果,结果就是undefined
2.匿名函数
let abs = function(x) {
if (x >= 0) {
return x;
} else {
return -x;
}
}
function(x){…}这是一个匿名函数,但是可以把结果赋值给abs,通过abs就可以调用函数!
六、日期对象
let now = new Date();//Sun May 30 2021 21:45:07 GMT+0800 (中国标准时间)
now.getFullYear();//年 2021
now.getMonth();//月 0-11
now.getDate();//日
now.getDay();//星期几 0-6 0:星期天
now.getHours();//时
now.getMinutes();//分
now.getSeconds();//秒
now.getTime();//时间戳 全世界统一 1970 1.1 00:00:00 毫秒数console.log(new Date(1622382307167));//时间戳转为时间