JavaScript(简称JS)是一种轻量级的编程语言,被广泛用于网页开发中,用于实现网页的交互功能。对于初学者来说,掌握JavaScript的基础语法是迈向成功的第一步。本文将为你详细介绍99种JavaScript的基础语法,帮助你轻松入门,让你的编程之路更加顺畅。
1. 变量声明
在JavaScript中,变量是用来存储数据的。声明变量有几种方式:
- 使用
var关键字:var age = 18; - 使用
let关键字(ES6引入):let score = 90; - 使用
const关键字(ES6引入):const PI = 3.14159;
2. 数据类型
JavaScript有8种基本数据类型:
- 数字(Number)
- 字符串(String)
- 布尔值(Boolean)
- 空值(Undefined)
- 空对象(Null)
- 对象(Object)
- 数组(Array)
- 函数(Function)
3. 运算符
JavaScript中的运算符包括:
- 算术运算符:加(+)、减(-)、乘(*)、除(/)、取余(%)、自增(++)、自减(–)
- 比较运算符:等于(==)、不等于(!=)、严格等于(===)、严格不等于(!==)、大于(>)、小于(<)、大于等于(>=)、小于等于(<=)
- 逻辑运算符:与(&&)、或(||)、非(!)
- 赋值运算符:等于(=)、加等于(+=)、减等于(-=)、乘等于(*=)、除等于(/=)、取余等于(%=)
4. 控制语句
- 条件语句(if…else)
if (age >= 18) { console.log("成年人"); } else { console.log("未成年人"); } - 循环语句(for、while、do…while)
for (let i = 0; i < 5; i++) { console.log(i); } - 跳转语句(break、continue)
5. 函数
函数是JavaScript的核心组成部分,用于封装一段可复用的代码:
function sayHello(name) {
console.log("Hello, " + name);
}
sayHello("Alice"); // 输出:Hello, Alice
6. 对象
对象是JavaScript中的一种数据结构,用于存储键值对:
let person = {
name: "Bob",
age: 25,
sayHello: function() {
console.log("Hello, " + this.name);
}
};
person.sayHello(); // 输出:Hello, Bob
7. 数组
数组是JavaScript中的一种有序集合,用于存储多个值:
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // 输出:Apple
8. 事件处理
JavaScript可以用来处理网页事件,如点击、鼠标移动等:
<button onclick="showMessage()">点击我</button>
<script>
function showMessage() {
alert("按钮被点击了!");
}
</script>
9. 99种基础语法总结
以上仅列举了JavaScript部分基础语法,以下是99种基础语法的简要总结:
- 变量声明
- 数据类型
- 运算符
- 控制语句
- 函数
- 对象
- 数组
- 事件处理
- 函数表达式
- 闭包
- 高阶函数
- 模块化编程
- Promise
- async/await
- 类与继承
- 模板字符串
- 解构赋值
- 扩展运算符
- 箭头函数
- Promise.all
- Promise.race
- Promise.finally
- Promise.reject
- Promise.resolve
- Array.from
- Array.of
- Array.isArray
- Array.prototype.copyWithin
- Array.prototype.fill
- Array.prototype.find
- Array.prototype.findIndex
- Array.prototype.keys
- Array.prototype.values
- Array.prototype.entries
- Array.prototype.includes
- Array.prototype.indexOf
- Array.prototype.lastIndexOf
- Array.prototype.map
- Array.prototype.filter
- Array.prototype.reduce
- Array.prototype.reduceRight
- Array.prototype.forEach
- Array.prototype.some
- Array.prototype.every
- Array.prototype.sort
- Array.prototype.reverse
- Array.prototype.shift
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.splice
- Array.prototype.unshift
- Array.prototype.concat
- Array.prototype.join
- Array.prototype.slice
- Array.prototype.toString
- Array.prototype.toLocaleString
- Array.prototype.toSource
- Array.prototype.valueOf
- Function.prototype.apply
- Function.prototype.call
- Function.prototype.bind
- Object.create
- Object.defineProperty
- Object.defineProperties
- Object.freeze
- Object.seal
- Object.assign
- Object.keys
- Object.values
- Object.entries
- Object.getOwnPropertyNames
- Object.getOwnPropertySymbols
- Object.getPrototypeOf
- Object.isExtensible
- Object.isFrozen
- Object.isSealed
- Object.is
- Object.preventExtensions
- Object.setPrototypeOf
- String.prototype.charAt
- String.prototype.charCodeAt
- String.prototype.concat
- String.prototype.includes
- String.prototype.indexOf
- String.prototype.lastIndexOf
- String.prototype.length
- String.prototype.localeCompare
- String.prototype.match
- String.prototype.matchAll
- String.prototype.padEnd
- String.prototype.padStart
- String.prototype.repeat
- String.prototype.replace
- String.prototype.search
- String.prototype.slice
- String.prototype.split
- String.prototype.startsWith
- String.prototype substr
- String.prototype.toLowerCase
- String.prototype.toUpperCase
掌握这些基础语法,将为你在JavaScript编程的道路上打下坚实的基础。祝你在编程之旅中一切顺利!
