var let const
var是全局作用,所以基本不用
let是变量
const是常量,但是如果声明的是数组或对象时,是可以部分改变,但不能完全改变
String Number Boolean null undefined
整型和浮点型都是Number类型
null和unfined区别为null是空,unfined是根本没有定义
const username = "Z";
const age = 30;
const isCool = true;
const rate = 4.5;
const x = unfined;
const y = null;
查看数据类型
const age = 30;
console.log(typeof age);
有两种方式
1.字符串+变量
2.使用`${变量}`
const age = 30;
console.log("My age is "+age);
console.log(`My age is ${age}`);
const s = "Hello JavaScritp,yes";
console.log(s.length);
console.log(s.toUpperCase());
console.log(s.toLowerCase());
console.log(s.substring(1,3));
console.log(s.split(""));
1.使用newArray()
const numbers = new Array(1,2,3,4);
console.log(numbers);
2.使用[]声明
const fruits = ["apple","pears"];
console.log(fruits);
const fruits = ["apple","pears"];
console.log(fruits[1]);
1.在数组末尾添加值
const fruits = ["apple","pears"];
fruits.push("banana");
// console.log(numbers);
console.log(fruits);
const fruits = ["apple","pears"];
fruits.unshift("banana");
// console.log(numbers);
console.log(fruits);
console.log(fruits[1]);
删除数组末尾元素
const fruits = ["apple","pears"];
fruits.pop();
console.log(fruits);
const fruits = ["apple","pears"];;
console.log(fruits);
const fruits = ["apple","pears"];
console.log(fruits.indexOf("pears"));
const person = {
firstName: "Vincent",
age: 26,
hobbies: ["games","movies"],
address: {
street: "six road",
city: "xian",
state: "MA"
}
};
console.log(person);
person.email = "[email protected]"
console.log(person.email);
const person = {
firstName: "Vincent",
age: 26,
hobbies: ["games","movies"],
address: {
street: "six road",
city: "xian",
state: "MA"
}
};
const {firstName,age} = person;
console.log(firstName);
const todos = [
{
id : 1,
text: "take out trash",
isComplete: true
},
{
id : 2,
text: "meeting with boss",
isComplete: false
},
{
id : 3,
text: "take out",
isComplete: true
}
];
console.log(todos);
const todos = [
{
id : 1,
text: "take out trash",
isComplete: true
},
{
id : 2,
text: "meeting with boss",
isComplete: false
},
{
id : 3,
text: "take out",
isComplete: true
}
];
console.log(todos[1].text);
const todoJSON = JSON.stringify(todos)
console.log(todos);
const x = 11;
const y = 2;
if(x === 10 || y>1){
console.log("x is 10");
}else if(x > 10){
console.log("x > 10")
}else{
console.log("x is not 10")
}
const x = 1;
const color = x>10 ? "red" :"blue";
console.log(color);
注意要带break
const color = "yellow";
switch (color){
case "red":
console.log("color is red");
break;
case "bule":
console.log("color is bule");
break;
default:
console.log("color is not found");
}
const todos = [
{
id : 1,
text: "take out trash",
isComplete: true
},
{
id : 2,
text: "meeting with boss",
isComplete: false
},
{
id : 3,
text: "take out",
isComplete: true
}
];
for(let i=0; i<todos.length; i++){
console.log(`x值为:${i}`);
console.log(todos[i]);
}
let y = 0;
while(y<todos.length){
console.log(`y值为:${y}`);
console.log(todos[y]);
y++;
}
for(let todo of todos){
console.log(todo.text);
}