JavaScript基础知识(一)

//字符串
console.log("hello".length);
console.log("hello".charAt(0));
console.log("hello world".replace("hello", "hi"));
console.log("hello".toUpperCase());

// 变量
//var是全局变量
var myname = "Das";
console.log(myname);
//let用在循环体 
let a = 1;
console.log(a);

var x;
x = "3" + 4 + 5; //345
//有了一个字符串,之后所有的都会被转化为字符串

x = 3 + 4 + "5"; //75

//比较
console.log(123 == "123");           //true
console.log(123 === "123");          //false
//==会自动进行类型转换 123=>“123” 1=>true
console.log(1 == true);          //true
console.log(1 === true);         //false

//条件
var age = 17;
var allowed = (age >= 18) ? "Yes" : "No";
console.log(allowed);

//switch
var myname = "John";
switch(myname){
    case "Simon":
        console.log("Simon");
        break;
    case "John":
        console.log("John");
        break;
    default :
        console.log("Stranger");
}

//对象
var obj = new Object();
var obj2 = {};

obj = {
    name: "Simon",
    age: 20,
    email:"123@qq.com",
    contact: {
        phone:"123456789",
        Telegram:"@Simon"
    }
};
//输出
console.log(obj.contact.phone);
//修改
obj.age = "21";
//添加
obj.contact.wechat = "sda"

console.log(obj);

//数组
var d = new Array();
var b = [];  

d[0] = "dog";
d[1] = "cat";
d[5] = "tiger";

b = ["dog","cat","tiger"];

b.push("fish"); 
b.pop();
b.shift();          //删除第一个位置的元素
b.unshift("fish");  //将fish添加到第一个元素

//函数
let c = 1;
function add(x){
    c += x;
}
add(2);

console.log(c);

function add_all() {
    let sum = 0;
    for(let i = 0, j = arguments.length; i < j; i++){
        sum +=arguments[i];
    }
    return sum;
}
let sum = add_all(1,2,3,4,5,6,7,8,9,10);
console.log(sum);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值