คำถามที่มีคนถามบ่อยที่สุดใน JavaScript
1️⃣ ตัวแปร var, let, const ต่างกันยังไง?
-
var
มี scope แบบ function (เก่า) -
let
มี scope แบบ block (ใหม่, ควรใช้) -
const
คล้ายlet
แต่เปลี่ยนค่าตัวแปรไม่ได้
2️⃣ JavaScript เป็น synchronous หรือ asynchronous?
-
โดยปกติ synchronous (ทำงานทีละบรรทัด)
-
แต่รองรับ asynchronous ผ่าน:
-
Callback
-
Promise
-
async/await
-
3️⃣ Closure คืออะไร?
ฟังก์ชันที่จำ state ของตัวแปรภายนอกได้
เช่น:
4️⃣ this ใน JavaScript หมายถึงอะไร?
-
this
คือ object ที่ function นั้นๆ ถูกเรียกผ่าน -
มีความแตกต่างเมื่อใช้ใน object, function, arrow function, class
-
ใน arrow function —
this
ไม่เปลี่ยนตาม context
5️⃣ Promise / Async Await ต่างกันยังไง?
-
Promise ใช้
.then()
.catch()
-
Async/Await ทำให้เขียน Promise ได้เหมือน synchronous
-
Async/Await สะอาด อ่านง่ายกว่า
6️⃣ == กับ === ต่างกันยังไง?
-
==
(Loose Equality) เช็คค่าโดยแปลง type อัตโนมัติ -
===
(Strict Equality) เช็คค่าและ type ต้องตรงกัน
ส่วนใหญ่ควรใช้ === เสมอ
7️⃣ Hoisting คืออะไร?
-
การย้ายตัวแปรและฟังก์ชันไปไว้ด้านบนก่อนรันจริง
-
var
ถูก hoist แต่ค่าเป็นundefined
-
let
กับconst
ก็ hoist แต่จะอยู่ใน Temporal Dead Zone
8️⃣ Event Loop คืออะไร?
-
กลไกของ JS ที่ช่วยจัดการ asynchronous
-
ทำงานผ่าน call stack, web API, task queue
9️⃣ Prototype คืออะไร?
-
กลไกเบื้องหลัง inheritance ของ JS
-
Object ทุกตัวใน JS มี prototype