
逐步使用代码示例来帮助您更好地理解每个概念。
1. js简介
javascript 是一种多功能语言,可以在浏览器或服务器上运行(使用 node.js)。它用于使网页具有交互性。
hello, javascript!
2. js 去哪里
javascript 可以放置在三个位置:
- 内联: ``html
点我
立即学习“Java免费学习笔记(深入)”;
2. **internal** (inside a `
- 外部(在外部 .js 文件中): ``html
console.log("这是外部js!");
--- ### **3. js output** different ways to display output in javascript: ```html
4. js 语句
声明是一条指令。例如:
let a = 5; // this is a statement let b = 6; // this is another statement let sum = a + b; // statement calculating the sum
5. js 语法
语法是指 javascript 程序如何构建的一组规则:
let name = "john"; // variable declaration and assignment console.log(name); // statement to output the value of 'name'
6. js评论
// this is a single-line comment /* this is a multi-line comment */ let x = 10; // this is an inline comment
7. js 变量
变量存储数据值。您可以使用 let、var 或 const 来声明它们。
let name = "alice"; // declaring a string variable var age = 30; // declaring a number variable const country = "usa"; // declaring a constant
8. js 让
let 关键字允许您声明具有块作用域的变量。
let x = 10;
if (x > 5) {
let y = 20; // 'y' only exists inside this block
}
console.log(y); // error: y is not defined
9. js 常量
用 const 声明的变量是不可变的(不能重新分配它们)。
const pi = 3.14159; pi = 3.14; // error: assignment to constant variable.
10。 js 运算符
运算符用于对变量和值执行运算:
let a = 10; let b = 20; let sum = a + b; // arithmetic operator let isequal = (a == b); // comparison operator let notequal = (a != b); // logical operator
11。 js 算术
javascript 提供了基本的算术运算符:
let x = 5; let y = 2; let sum = x + y; // addition let difference = x - y; // subtraction let product = x * y; // multiplication let quotient = x / y; // division let remainder = x % y; // modulus (remainder of division)
12。 js 作业
赋值运算符赋值:
let x = 10; // assign 10 to x x += 5; // add 5 to x (x = x + 5) x *= 2; // multiply x by 2
13。 js 数据类型
javascript 有多种数据类型:
let name = "alice"; // string
let age = 25; // number
let isstudent = true; // boolean
let person = {name: "john", age: 30}; // object
let colors = ["red", "blue", "green"]; // array
let notdefined; // undefined
let emptyvalue = null; // null
14。 js 函数
函数是设计用于执行任务的代码块。
function greet(name) {
return "hello " + name;
}
console.log(greet("alice")); // output: hello alice
15。 js 对象
javascript 对象是键值对。
let person = {
name: "john",
age: 30,
greet: function() {
console.log("hello, " + this.name);
}
};
person.greet(); // output: hello, john
16。 js 对象属性
使用点表示法或方括号表示法访问对象属性:
let car = {make: "toyota", model: "corolla"};
console.log(car.make); // output: toyota
console.log(car["model"]); // output: corolla
17。 js 对象方法
对象方法是存储为对象属性的函数:
let person = {
name: "alice",
sayhello: function() {
return "hello, " + this.name;
}
};
console.log(person.sayhello()); // output: hello, alice
18。 js 对象构造函数
对象构造函数允许您创建具有相同结构的多个对象:
function person(name, age) {
this.name = name;
this.age = age;
}
let john = new person("john", 30);
console.log(john.name); // output: john
这些是 javascript 基础知识的前几个示例,带有解释和代码。如果您想深入了解数组、循环、字符串操作等特定部分,请告诉我!
让我们继续逐步了解 javascript 概念的完整列表以及代码示例:
19。 js 事件
javascript 可以响应各种用户事件,例如单击、鼠标移动或按键。
20。 js 字符串
javascript 中的字符串是字符序列。
let greeting = "hello, world!"; let length = greeting.length; // string length let uppercasegreeting = greeting.touppercase(); // convert to uppercase console.log(uppercasegreeting); // output: hello, world!
21。 js 字符串方法
javascript 提供了几种内置的字符串方法:
let str = "hello, world!";
let substr = str.substring(0, 5); // extract substring
let replacedstr = str.replace("world", "everyone"); // replace part of string
console.log(replacedstr); // output: hello, everyone!
22。 js 字符串搜索
您可以在 javascript 字符串中搜索子字符串:
let sentence = "javascript is awesome!";
let position = sentence.indexof("awesome"); // finds the position of the word 'awesome'
console.log(position); // output: 15
23。 js 字符串模板
模板文字允许在字符串中嵌入变量和表达式。
let name = "alice";
let greeting = `hello, ${name}!`; // template literal using backticks
console.log(greeting); // output: hello, alice!
24。 js 数字
javascript 处理整数和浮点数。
let num1 = 5; let num2 = 2.5; let sum = num1 + num2; // adding numbers console.log(sum); // output: 7.5
25。 js bigint
bigint 用于任意大的整数。
let bignumber = bigint("123456789012345678901234567890");
console.log(bignumber); // output: 123456789012345678901234567890n
26。 js 数字方法
javascript 提供了数字方法,例如:
let num = 9.656; let rounded = num.tofixed(2); // rounds to 2 decimal places console.log(rounded); // output: 9.66
27。 js 数字属性
javascript 具有内置的数字属性:
let max = number.max_value; // largest possible number in js console.log(max); // output: 1.7976931348623157e+308
28。 js 数组
数组是值的列表:
let fruits = ["apple", "banana", "cherry"]; console.log(fruits[1]); // output: banana
29。 js 数组方法
javascript 数组具有多种方法:
let fruits = ["apple", "banana", "cherry"];
fruits.push("mango"); // adds an element at the end
let lastfruit = fruits.pop(); // removes the last element
console.log(lastfruit); // output: mango
30。 js 数组搜索
您可以搜索数组中的值:
let numbers = [1, 2, 3, 4, 5]; let index = numbers.indexof(3); // find the index of 3 console.log(index); // output: 2
31。 js 数组排序
您可以使用 sort() 对数组进行排序:
let fruits = ["banana", "apple", "cherry"]; fruits.sort(); // sort alphabetically console.log(fruits); // output: ["apple", "banana", "cherry"]
32。 js 数组迭代
使用循环或数组方法迭代元素:
let fruits = ["apple", "banana", "cherry"]; fruits.foreach((fruit) => console.log(fruit)); // output each fruit
33。 js 数组常量
用 const 声明的数组仍然可以修改其内容:
const fruits = ["apple", "banana"];
fruits.push("cherry"); // this works
console.log(fruits); // output: ["apple", "banana", "cherry"]
34。 js 日期
javascript 提供了 date 对象来处理日期:
let date = new date(); console.log(date); // output: current date and time
35。 js 日期格式
您可以用不同的方式格式化日期:
let date = new date(); console.log(date.todatestring()); // output: mon sep 28 2024
36。 js 日期获取方法
javascript 提供了获取部分日期的方法:
let date = new date(); let year = date.getfullyear(); // get the year let month = date.getmonth(); // get the month (0-11) console.log(year, month); // output: 2024, 8 (september)
37。 js 日期设置方法
您可以设置日期的特定部分:
let date = new date(); date.setfullyear(2025); // set the year to 2025 console.log(date); // output: updated date with the new year
38。 js 数学
javascript 提供了一个用于数学运算的 math 对象:
let x = math.pi; // value of pi let y = math.sqrt(16); // square root of 16 console.log(x, y); // output: 3.141592653589793, 4
39。 js随机
您可以在 javascript 中生成随机数:
let randomnum = math.random(); // generates a number between 0 and 1 console.log(randomnum);
40。 js 布尔值
布尔值可以有两个值:true 或 false。
let isjavascriptfun = true; let isfishtasty = false; console.log(isjavascriptfun); // output: true
41。 js 比较
比较运算符用于比较值:
let a = 10; let b = 5; console.log(a > b); // true console.log(a == b); // false console.log(a !== b); // true
42。 js if else
条件语句允许您根据条件执行代码:
let age = 18;
if (age >= 18) {
console.log("you are an adult.");
} else {
console.log("you are not an adult.");
}
43。 js 切换
switch 语句允许您根据值执行不同的代码块:
let fruit = "apple";
switch (fruit) {
case "banana":
console.log("banana");
break;
case "apple":
console.log("apple");
break;
default:
console.log("unknown fruit");
}
44。 js 循环
for 循环允许您重复代码指定的次数:
for (let i = 0; i < 5; i++) {
console.log(i); // output: 0, 1, 2, 3, 4
}
45。 js 循环 for in
for-in 循环迭代对象的属性:
let person = {name: "john", age: 30, city: "new york"};
for (let key in person) {
console.log(key + ": " + person[key]);
}
46。 js 循环 for
for-of 循环用于迭代可迭代对象,例如数组:
let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
console.log(fruit); // output each fruit
}
47。 js 循环 while
只要条件为真,while 循环就会执行:
let i = 0;
while (i < 5) {
console.log(i); // output: 0, 1, 2, 3, 4
i++;
}
48。 js 休息
break 用于退出循环:
for (let i = 0; i < 5; i++) {
if (i === 3) break;
console.log(i); // output: 0, 1, 2
}
49。 js 可迭代
可以迭代的对象称为可迭代对象,如数组、字符串、映射等。
let str = "Hello";
for (let char of str) {
console.log(char); // Output each character
}
这是 javascript 教程的延续,涵盖下一组概念。如果您还想进一步了解,请告诉我
详细信息或任何说明!










