一、变量
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>hello world</h1>
<script>
console.log('hello China!');//打印字符串
let age = 12; //变量
const num = "123";//常量
console.log(typeof(num));//打印变量类型
</script>
</body>
</html>
二、对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>hello world</h1>
<script>
// 创建一个包含多个属性的对象
var result = {
name: "John Doe",
id: 123,
personlist: ["Alice", "Bob", "Charlie"]
};
// 打印对象的内容
console.log("Result object:", result);
console.log("Name:", result.name);
console.log("ID:", result.id);
console.log("Person list:", result.personlist);
</script>
</body>
</html>
三、数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array Operations in Script</title>
</head>
<body>
<h1>Array Operations Example</h1>
<script>
// 定义包含对象的数组
var data = [
{ id: 1, name: "Alice", score: 85 },
{ id: 2, name: "Bob", score: 72 },
{ id: 3, name: "Charlie", score: 90 }
];
// 打印第二个元素
console.log("Second element:", data[1]);
// 打印第二个元素的名字
console.log("Second element:", data[1].name);
// 遍历并打印所有元素
console.log("Printing all elements:");
data.forEach(
function(item)
{
console.log("ID:", item.id, "Name:", item.name, "Score:", item.score);
});
//转化为json输出(key也带双引号)
var jsonData = JSON.stringify(data);
console.log(jsonData);
</script>
</body>
</html>