//字符串
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);
JavaScript基础知识(一)
最新推荐文章于 2024-10-18 17:51:23 发布