1.js的安装与调试
console.log('hello world');
simple math excute;
For example:1+1
alert('123');
js编辑器:code.visualstudio.com
node:
8.数组
const fruits=['apples','bears','oranges'];
fruits[3]='banana';//自定义位置添加元素
fruits.push('mangos');//尾部添加元素
fruits.unshift('watermelon');//头部添加元素
fruits.pop();//删除末尾元素
console.log(fruits);
console.log(Array.isArray(fruits));//判断fruits是不是数组
console.log(fruits.indexOf('oranges'));//筛选oranges是该数组第几个元素
9.对象
console.log('hello world');
const person={
firstName:'John',
lastName:'Doe',
age:30,
hobbies:['music','watch movies','sports'],
address:{
street:'50 main st',
city:'Boston',
state:'Ma'
}
}
person.email='John@gmail.com';//向person中添加属性
console.log(person);
console.log(person.firstName,person.lastName);
console.log(person.address.city,person.hobbies[2]);
const{firstName,lastName,address:{street}}=person;//将person内同名的变量传出给全局同名变量
console.log(firstName,lastName,street);
9.对象数组和JSON
const todos=[
{
id:1,
text:'Take our trash',
isCompleted:true,
},
{
id:2,
text:'meeting with boss',
isCompleted:true,
},
{
id:3,
text:'write reports',
isCompleted:false,
}
]
console.log(todos);
console.log(todos[1].text);//打印对象数组中ID:2的text内容
const todoJSON =JSON.stringify(todos);
console.log(todoJSON);//这两行是把js变成JSON输出
10.if语句
const x=4;
const y=10;
if(x===10||y>10){//===判断数值时同时判断数据类型,||表示或
console.log('x is 5 or y is more than 10');
}
else if(x>10){
console.log('x is greater than 10')
}
else{
console.log('x is less than 10')
}
11.三目运算符
const x=10;
const color =x>10?'red' :'blue';//如果x大于10,则color为red,否则为blue
console.log(color);
12.swich
//For
for(let i =0;i<10;i++){//(初始化,循环条件,执行语句)
console.log(i)
}
//while
let i=0;
while(i<10){
console.log('i='+i);
i++;
}