data type...???
Datatype in JavaScript is the type of values that a variable can store. Knowing and understanding data types allows you to act on them correctly.
Primitive Data Types
JavaScript has 7 basic data types:
String 📝
Used to store text or sequence of characters.
Must be surrounded by single (') or double (") quotation marks or Backticks (") for Template Literals
Example: 'Hello world', "JavaScript," Name: ${name}
Number 🔢
Used to store numbers, both integers and decimals.
There are special values like Infinity, -Infinity and NaN (Not a Number).
Example: 10, 3.14, -50
BigInt 🐘
Used to store integers larger than the Number can support (over 2
53
- 1)
Must be appended to the letter n.
Example: 9007199254740991n, 12345678901234567890n
Boolean ✅
It is used to store logical values. There are only two values: true (true) and false (false).
Often used in decision making (Conditional logic)
Example: true, false
Undefined. ❓
Refers to a variable that has already been declared but has not yet been configured.
It is the default value of the recently announced variable.
Example: Let x; / / x have an undefined value.
Null. 🚫
It means no value or empty value.
The developer determines that value itself to show that the variable does not have any value.
Example: let data = null;
Symbol ✨
It is a data type added to ES6 (ECMAScript 2015).
Used to create unique identifiers to use as Object properties.
Reference Data Type (Non-Primitive / Reference Data Type)
JavaScript has the main reference data types:
Object 🧱
Used to store collections of complex data (key-value pairs)
It is the foundation of complex data structures in JavaScript, including Array and Function.
Example:
Plain Object: let person = {name: "Alice," age: 30};
Array: let colors = ["red," "green," "blue"]; (Technically, an Array is a kind of Object)
Function: function greet () {/ * code * /} (Function is also considered a kind of Object)
Remarks:
Primitive types store value directly (By value)
Reference types store a reference (Address) to the location of the value in memory (By reference).
Understanding these data types is critical in writing accurate and efficient JavaScript code!
เมื่อคุณเริ่มเขียนโค้ดใน JavaScript การเข้าใจประเภทข้อมูลหรือ datatype เป็นสิ่งสำคัญมาก เพราะช่วยให้จัดการกับตัวแปรและค่าได้อย่างถูกต้องและมีประสิทธิภาพ นอกจากข้อมูลพื้นฐาน 7 ประเภทที่กล่าวถึงในเนื้อหาแล้ว JavaScript ยังเน้นถึงประเภทข้อมูลที่ไม่เหมือนกันในแง่ของการจัดเก็บข้อมูล ได้แก่ ประเภทข้อมูลพื้นฐาน (Primitive types) ที่เก็บค่าแบบตรง ๆ เช่น String หรือ Number และประเภทข้อมูลแบบอ้างอิง (Reference types) ที่เก็บเป็นที่อยู่ของข้อมูลในหน่วยความจำ ซึ่งเข้าใจความแตกต่างนี้ช่วยหลีกเลี่ยงข้อผิดพลาดเมื่อเปลี่ยนแปลงค่าของตัวแปรที่เป็น Object หรือ Array อีกสิ่งสำคัญคือ datatype Symbol ซึ่งถูกเพิ่มเข้ามาตั้งแต่ ES6 โดยใช้สร้างตัวระบุที่ไม่ซ้ำกัน ทำให้สามารถใช้เป็นกุญแจ (key) ใน Object ได้โดยไม่ต้องกลัวชนกันของชื่อสมบัติเหมือนการใช้ String เรื่อง BigInt เป็นประโยชน์มากเมื่อคุณต้องจัดการกับจำนวนเต็มที่ใหญ่เกินกว่าค่าที่ประเภท Number สามารถเก็บได้ ตัวอย่างเช่น การจัดเก็บเลขประจำตัวประชาชนหรือค่าทางการเงินขนาดใหญ่ที่ต้องการความแม่นยำสูง การมีความรู้เรื่องค่า Undefined และ Null ก็ช่วยให้คุณเข้าใจสถานะของตัวแปรระหว่างเขียนโค้ด เช่น Undefined หมายถึงตัวแปรที่ถูกประกาศแต่ยังไม่ได้กำหนดค่า ขณะที่ Null ใช้แสดงเจตนาเพื่อว่างเปล่าหรือไม่มีค่าอะไร การใช้งาน object ใน JavaScript มีความยืดหยุ่นสูง โดยสามารถเก็บข้อมูลในรูปแบบ key-value pairs และยังสามารถสร้างข้อมูลเชิงซ้อนอย่าง Array หรือ Function ได้ ซึ่งเป็นความสามารถที่สำคัญในการจัดการข้อมูลและสร้างโปรแกรมที่มีโครงสร้างดี หากคุณสนใจเพิ่มพูนความรู้เกี่ยวกับการใช้งาน datatype ในสถานการณ์จริง การทดลองเขียนโค้ดและทดสอบผลลัพธ์ด้วย typeof operator จะช่วยให้เห็นภาพชัดเจนขึ้นว่าข้อมูลแต่ละชนิดทำงานอย่างไร และช่วยให้คุณพัฒนาทักษะเขียนโค้ด JavaScript ที่ถูกต้องและทันสมัยมากขึ้น










































































































