example
example

JS JS 语法

  • 主题发起人 主题发起人 Yann
  • 开始时间 开始时间

Yann

站长
管理成员
GTAOS管理组
认证卖家
认证用户
黄金
192.55G
1.变量声明和使用:
JavaScript:
var name = "John";
let age = 25;
console.log("My name is " + name + " and I am " + age + " years old.");

2.条件语句(if-else):
JavaScript:
let score = 85;
if (score >= 60) {
console.log("You passed the exam!");
} else {
console.log("You failed the exam.");
}

3.循环语句(for):
JavaScript:
for (let i = 1; i <= 5; i++) {
console.log("Number: " + i);
}

4.数组声明和访问:
JavaScript:
let fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // 输出 "apple"

5.函数声明和调用:
JavaScript:
function greet(name) {
console.log("Hello, " + name + "!");
}

greet("Alice"); // 输出 "Hello, Alice!"

6.对象声明和访问:
JavaScript:
let person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello, my name is " + this.name + "!");
    }
};

console.log(person.name);   // 输出 "John"
console.log(person.age);    // 输出 30
person.greet();             // 输出 "Hello, my name is John!"

7.文件导入和模块使用:
JavaScript:
// 文件1.js
export const variable = "Hello from file 1!";

// 文件2.js
import { variable } from './1.js';
console.log(variable); // 输出 "Hello from file 1!"

8.异常处理:
JavaScript:
function divide(numerator, denominator) {
if (denominator === 0) {
throw new Error("Division by zero error!");
    }
 return numerator / denominator;
}

try {
console.log(divide(10, 0));
} catch (error) {
console.log("Error: " + error.message);
}
 
最后编辑:
后退
顶部