JavaScript เบื้องต้น
JavaScript คือภาษาของ Node.js
หัวข้อที่มีชื่อว่า “JavaScript คือภาษาของ Node.js”Node.js คือ runtime ที่รัน JavaScript นอก browser ทุกอย่างที่เขียนใน Node ตั้งแต่ HTTP server ง่ายๆ ไปจนถึง CLI tool ที่ซับซ้อน ล้วนเป็น JavaScript ดังนั้นการเข้าใจภาษาอย่างแน่นหนาจึงเป็นสิ่งจำเป็นก่อนที่จะลงลึกใน API ของ Node
โมดูลนี้ครอบคลุม JavaScript พื้นฐานที่นักพัฒนามักสะดุดมากที่สุดในระบบนิเวศ Node:
| บทเรียน | หัวข้อ |
|---|---|
| Types และ Coercion | Primitives vs objects, typeof, == vs ===, truthy/falsy |
| Scope และ Closures | var/let/const, block scope, hoisting, lexical scope |
this และ Context | Dynamic this, call/apply/bind, arrow functions |
| Modules | ESM import/export vs CommonJS require, ใช้เมื่อไหร่ |
JavaScript สมัยใหม่หมายถึง ES2022 ขึ้นไป ตลอดทั้งโมดูล — const/let, arrow functions, destructuring, async/await, optional chaining และ native ES modules โมดูลนี้ใช้ Node 20+
Demo แรกที่รันได้
หัวข้อที่มีชื่อว่า “Demo แรกที่รันได้”โค้ดด้านล่างแสดงแนวคิด 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));}