ข้ามไปยังเนื้อหา

JavaScript เบื้องต้น

Node.js คือ runtime ที่รัน JavaScript นอก browser ทุกอย่างที่เขียนใน Node ตั้งแต่ HTTP server ง่ายๆ ไปจนถึง CLI tool ที่ซับซ้อน ล้วนเป็น JavaScript ดังนั้นการเข้าใจภาษาอย่างแน่นหนาจึงเป็นสิ่งจำเป็นก่อนที่จะลงลึกใน API ของ Node

โมดูลนี้ครอบคลุม JavaScript พื้นฐานที่นักพัฒนามักสะดุดมากที่สุดในระบบนิเวศ Node:

บทเรียนหัวข้อ
Types และ CoercionPrimitives vs objects, typeof, == vs ===, truthy/falsy
Scope และ Closuresvar/let/const, block scope, hoisting, lexical scope
this และ ContextDynamic this, call/apply/bind, arrow functions
ModulesESM import/export vs CommonJS require, ใช้เมื่อไหร่

JavaScript สมัยใหม่หมายถึง ES2022 ขึ้นไป ตลอดทั้งโมดูล — const/let, arrow functions, destructuring, async/await, optional chaining และ native ES modules โมดูลนี้ใช้ Node 20+

โค้ดด้านล่างแสดงแนวคิด JS หลายอย่างพร้อมกัน: การประกาศ const, ฟังก์ชัน, การต่อสตริงแบบไม่ใช้ template literal และ closure เหนือตัวแปรภายนอก ลองรันและสังเกตผลลัพธ์

const greet = function(name) {
return 'Hello, ' + name + '!';
};
const languages = ['JavaScript', 'TypeScript', 'Node.js'];
for (const lang of languages) {
console.log(greet(lang));
}
JavaScript
Node.js ให้สิ่งที่ browser ไม่มีคืออะไร?
โมดูลนี้พึ่งพาฟีเจอร์ JavaScript เวอร์ชันใด?