js入门

// 单行注释
/* 多行
注释 */

// 声明变量
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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容