// 单行注释
/* 多行
注释 */
// 声明变量
var a = "Hello";
let b = "World";
const pi = 3.14;
// 数组
var array = [1, 2, 3, 4, 5];
// 对象
var obj = {
name: "John Doe",
age: 30,
isMarried: true,
children: ['Jane', 'Joe'],
greet: function() {
console.log("Hello, " + this.name);
}
};
obj.greet(); //调用对象中的函数
// 条件语句
if (obj.age > 20) {
console.log("John is old.");
} else {
console.log("John is young.");
}
// 循环
for (let i = 0; i < 10; i++) {
console.log(i);
}
// 函数
function add(x, y) {
return x + y;
}
console.log(add(5, 3)); // 输出 8
// 异步操作
setTimeout(function() {
console.log("This message is delayed by 3 seconds");
}, 3000);
// Promise
let testPromise = new Promise(function(resolve, reject) {
a > 10 ? resolve(a) : reject('a is less than 10');
});
testPromise
.then(number => console.log('Promise Resolved: ' + number))
.catch(err => console.log('Promise Rejected: ' + err));
// ES6 箭头函数
const multiply = (x, y) => x * y;
console.log(multiply(3, 4)); // 输出 12
js入门
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 祝开卷有益。 这是一些学生出去面试时常见的一部分笔试题,我会说明每题考察的大概是什么,具体请自行查阅资料。 1.变...
- -- 《Node.js入门经典》,由George Ornbo创作,是一本极好的Node.js入门读物。本文为读该书...
- 姓名:房小慧 学号:17101223361 专业:软件工程 【嵌牛导读】:学Web前端的人都会用到JavaScri...