⚡ 编程实验室🏗️ HTML🎨 CSS⚡ JavaScript🐍 Python🗄️ SQL☕ Java⚛️ React💚 Vue🟢 Node.js⚙️ C语言🐘 PHP🐹 Go🔷 TypeScript🐬 MySQL🔧 C++🎯 C#🦀 Rust🅱️ Bootstrap💡 jQuery🎸 Django🍃 MongoDB👗 Sass🎪 Kotlin📊 R语言📋 XML📊 Excel🐘 PostgreSQL🐳 Docker🅰️ Angular🎮 游戏🏠 网站首页

JavaScript 运算符

学习算术、比较、逻辑运算符 · 难度:入门 · +10XP

JavaScript 运算符

JavaScript 运算符 — 编程的基本运算

学习前的准备

打开浏览器控制台(F12)。本教程适合 JavaScript 初学者。运算符是编程语言中最基础的组成部分——如果说变量是积木块,运算符就是连接这些积木的胶水。

算术运算符

console.log(10 + 5);   // 15  加法
console.log(10 - 5);   // 5   减法
console.log(10 * 5);   // 50  乘法
console.log(10 / 5);   // 2   除法
console.log(10 % 3);   // 1   取余(模运算)
console.log(2 ** 3);   // 8   指数运算

let x = 5; x++; // 6 后置自增 ++x; // 7 前置自增

赋值运算符

let num = 10;
num += 5;   // num = num + 5  → 15
num -= 3;   // num = num - 3  → 12
num *= 2;   // num = num * 2  → 24

比较运算符

console.log(5 > 3);    // true
console.log(5 == '5');   // true(类型转换)
console.log(5 === '5');  // false(严格比较)
// 建议:始终使用 === 和 !==

逻辑运算符

// && 逻辑与:两边都为 true 才返回 true
console.log(5 > 3 && 10 < 20);  // true

// || 逻辑或:只要有一边为 true 就返回 true console.log(5 < 3 || 10 > 5); // true

// ! 逻辑非:取反 console.log(!(5 > 3)); // false

// 短路求值 let name = ''; console.log(name || '匿名'); // '匿名'

typeof 和 instanceof

console.log(typeof 42);        // 'number'
console.log(typeof 'hello');   // 'string'
console.log(typeof null);      // 'object'(历史遗留bug)
console.log([1,2,3] instanceof Array);  // true

小结

运算符是编程的核心操作。算术运算符做数学计算,比较运算符判断大小关系(用 === 而非 ==),逻辑运算符组合条件,赋值运算符简化重复操作。建议你现在就打开控制台,试试各种运算符的组合效果。

Ctrl+Enter
🚀 升级VIP
解锁全部课程+AI助手

🏆 学习排行

加载中...

📊 统计

📖 231 篇
0 完成
🔥 0