Automatically translated.View original post

variable # JavaScript

# Web Site Writing Basics

# javascript # variable

Variables in JavaScript are storage boxes used to store values that can be cited and used in programs. 📦

It's like labeling a box to hold things. Here, "box" is a variable and "thing" is a data value (e.g. numbers, text, logic).

Declaration of variables in JavaScript

Declaring variables in JavaScript can be done using three major keywords, each of which has different functional characteristics and scope (Scope):

1. Let

❇ Use for: Declare a variable whose value can be changed later

❇ Scope (Scope): It is a Block Scope type, meaning that the variable will be accessible only within the code block ({}) being declared.

❇ Example:

JavaScript

Let age = 30;

Age = 31; / / can change the value.

2. Const

❇ Use for: Declare Constant (Constant), which must default immediately and cannot be changed later

❇ Scope (Scope): It's a Block Scope type, just like let

❇ Example:

JavaScript

Const PI = 3.14159;

/ / PI = 3.14; / / An error will occur because the const cannot be changed.

❇ ⚠️ Exception: For Objects and Arrays declared with that const, the values inside Object or Array can also be changed (but not all new variable values can be configured)

3. Var

❇ Use for: It is an old-style keyword used to declare variables (before the ES6 / ES2015 era)

❇ Scope (Scope): It is Function Scope or Global Scope, which is overly flexible and can easily lead to errors.

❇ Instructions: To write modern JavaScript code, let and const are recommended instead of var to control the scope of variables more clearly.

2025/9/30 Edited to

... Read moreใน JavaScript นอกจากการประกาศตัวแปรด้วย let, const และ var แล้ว การเข้าใจเกี่ยวกับชนิดข้อมูลและวิธีการจัดการตัวแปรจะช่วยเพิ่มประสิทธิภาพในการเขียนโปรแกรมอย่างมาก ตัวแปรใน JavaScript สามารถเก็บข้อมูลได้หลายประเภท ได้แก่ ตัวเลข (Number), ข้อความ (String), ค่าตรรกะ (Boolean), อ็อบเจ็กต์ (Object), และอาเรย์ (Array) ซึ่งแต่ละประเภทสามารถนำไปใช้เพื่อแก้ไขปัญหาหรือพัฒนาฟังก์ชันต่าง ๆ ได้ ตัวอย่างเช่น การใช้ตัวแปรชนิดอาเรย์เพื่อเก็บรายการข้อมูล หรือใช้วัตถุเพื่อจัดเก็บข้อมูลหลายๆ ตัวในที่เดียว นอกจากนี้ ขอบเขตของตัวแปร (Scope) ถือเป็นสิ่งสำคัญที่ต้องเข้าใจ เพราะส่งผลต่อการเข้าถึงและการทำงานของตัวแปรในโปรแกรม ตัวแปรที่ประกาศด้วย let และ const จะมีขอบเขตแบบ block scope คือจะใช้ได้เฉพาะในบล็อกที่ประกาศเท่านั้น การใช้ scope แบบนี้ช่วยลดความผิดพลาดที่เกิดจากตัวแปรชนิดเดียวกันถูกประกาศซ้ำหรือใช้ผิดที่ ส่วน var มีขอบเขตแบบ function scope ซึ่งอาจส่งผลให้เกิดปัญหาด้านความชัดเจนของตัวแปร ถ้าไม่ระมัดระวัง การใช้ let และ const จึงถือเป็นแนวทางที่แนะนำใน JavaScript สมัยใหม่ เพราะช่วยให้โค้ดปลอดภัยและดูแลรักษาง่ายขึ้น เมื่อพูดถึง const ซึ่งเป็นค่าคงที่นั้น ควรจำไว้ว่าถึงแม้ตัวแปรจะไม่สามารถเปลี่ยนแปลงค่าตัวแปรได้โดยตรง แต่ถ้าเป็นอ็อบเจ็กต์หรืออาเรย์ที่ประกาศด้วย const ข้อมูลภายในยังสามารถเปลี่ยนแปลงหรือปรับปรุงได้ตามต้องการ นี่เป็นความสามารถพิเศษที่ผู้พัฒนาควรรู้เพื่อใช้ประโยชน์จาก const อย่างถูกต้อง ท้ายที่สุด การฝึกปฏิบัติและทดสอบการประกาศตัวแปรในโปรแกรมจริงจะช่วยให้เข้าใจการทำงานของแต่ละชนิดตัวแปรได้ชัดเจนขึ้น การเขียนโค้ด JavaScript อย่างมีประสิทธิภาพรวมถึงการเลือกใช้ตัวแปรชนิดที่เหมาะสมและการควบคุมขอบเขตตัวแปรจะช่วยลดบั๊กและทำให้โปรแกรมทำงานได้อย่างราบรื่น