DEV Community

Cover image for 11+2 รูปแบบที่น่าจำ Truthy and Falsy และ  Primitive Type & Reference Type
Nantipat
Nantipat

Posted on • Edited on

1

11+2 รูปแบบที่น่าจำ Truthy and Falsy และ Primitive Type & Reference Type

กำลังอ่าน What the fuck js แล้วเลยไปเจอพวกนี้มาเลยต้องจดใส่สมองไว้หน่อย

Truthy Value

ก็คือค่าที่ถือว่าเป็น true เมื่อใช้ในการเปรียบเทียบ boolean ซึ่งมีด้วยกันทั้งหมด 5 ค่าที่เราควรระมัดระวัง

  • '0' string
  • 'false' string
  • [] empty array
  • {} empty object
  • ()=>{} empty function

Falsy Value

ก็คือค่าที่ถือว่าเป็น false เมื่อใช้ในการเปรียบเทียบ boolean ซึ่งมีด้วยกันทั้งหมด 6 ค่าที่เราควรระมัดระวัง

  • false — ค่า boolean ที่เป็น false
  • 0 — เลข 0 ที่มี type เป็น number
  • "", '', `` — empty string
  • null
  • undefined
  • NaN

null และ undefined

ค่าของสองตัวนี้จะเท่ากัน และเท่ากันกับค่าของตัวเองเท่านั้น

NaN

ค่าของตัวนี้อภิมหาพิเศษเพราะนอกจากมันจะไม่เท่ากับค่าใดๆแล้ว มันยังไม่เท่ากันกับค่าตัวเองอีกด้วย (โอ้โห!)

Primitive Type & Reference Type

Primitive Type คือเมื่อสร้างตัวแปรใหม่โดยอ้างอิงจากตัวแปรเก่า ค่าที่ได้จะเป็นการคัดลอก

Javascript-DataType

Reference Type คือเป็นการสร้างตัวแปรแบบอ้างอิง โดยที่เมื่อตัวแปรไหนเปลี่ยนตัวแปรอื่นก็จะเปลี่ยนตามด้วย จะใช้กับทั้ง Array และ Object

Ref:

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post →

Top comments (1)

Collapse
 
hambalee profile image
hambalee

ขอบคุณครับ

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay