<script> for (var i = 0; i < 5; i++) { console.log(i); } console.log("循环外:" + i) </script>
结果:
修改为let
for (let i = 0; i < 5; i++) { console.log(i); } console.log("循环外:" + i)
说明:把变量的作用域控制在循环内部
const :相当于定义一个常量
<script> const str = "abc"; console.log(str); console.log("-------------"); str = "123"; // 改值 console.log(str); </script>
说明:const :相当于定义一个常量,不允许修改
是否包含:
let str = "hello hll"; console.log(str,"中是否包含了 abc", str.includes("abc")) console.log(str,"中是否包含了 abcd", str.includes("abc"))
以什么开头和结尾
let str = "hello hll"; console.log(str,"中是否包含了 hll", str.includes("hll")) console.log(str,"中是否包含了 hlld", str.includes("hll")) console.log(str,"中是否以hll开头", str.startsWith("hll")) console.log(str,"中是否以hll开头", str.startsWith("hll")) console.log(str,"中是否以hll结尾", str.endsWith("hll")) console.log(str,"中是否以hll结尾", str.includes("hll"))
字符串扩展
结构表达式
let arr = "hello hll"; let [x, y] = arr; console.log(x, y)
const person = { name: "jack", age: 21, language: ['a', 'b', 'c'] }; let {name, age, language} = person; console.log(name, age, language)