javascript入门

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++;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值