Variables & Types
Variables & Types
Section titled “Variables & Types”การประกาศตัวแปร
Section titled “การประกาศตัวแปร”JavaScript มี 3 วิธีประกาศตัวแปร:
// const — ค่าคงที่ เปลี่ยน reassign ไม่ได้const API_URL = "https://api.example.com";
// let — ตัวแปรที่เปลี่ยนค่าได้ (block-scoped)let count = 0;count = 1; // ✅ OK
// var — แบบเก่า (function-scoped) ❌ หลีกเลี่ยงvar oldWay = "avoid this";# Python ไม่มี keyword แยก — ใช้ UPPER_CASE สำหรับ constantAPI_URL = "https://api.example.com"count = 0// C ใช้ const qualifierconst char* API_URL = "https://api.example.com";int count = 0;-- SQL ใช้ DECLARE สำหรับตัวแปร (T-SQL)DECLARE @count INT = 0;Primitive Types (7 ชนิด)
Section titled “Primitive Types (7 ชนิด)”| Type | ตัวอย่าง | typeof |
|---|---|---|
string | "hello", 'world' | "string" |
number | 42, 3.14, NaN | "number" |
boolean | true, false | "boolean" |
null | null | "object" ⚠️ |
undefined | undefined | "undefined" |
symbol | Symbol("id") | "symbol" |
bigint | 9007199254740991n | "bigint" |
Template Literals
Section titled “Template Literals”const name = "สมชาย";const age = 25;
// แบบเก่า — string concatenationconst msg1 = "สวัสดี " + name + " อายุ " + age + " ปี";
// แบบใหม่ — template literal (backtick)const msg2 = `สวัสดี ${name} อายุ ${age} ปี`;
// multiline ได้เลยconst html = ` <div> <h1>${name}</h1> <p>Age: ${age}</p> </div>`;typeof Operator
Section titled “typeof Operator”console.log(typeof "hello"); // "string"console.log(typeof 42); // "number"console.log(typeof true); // "boolean"console.log(typeof undefined); // "undefined"console.log(typeof null); // "object" ⚠️console.log(typeof [1, 2, 3]); // "object" (array เป็น object)console.log(typeof { a: 1 }); // "object"