Automatically translated.View original post

null. nan. undefined

# Web Site Writing Basics

# javascript

# null_ nan _ undefined

The null variable in JavaScript is a value that represents no intentional value or that the variable does not point to any object.

Important characteristics of null

Data Type (Type): Although null means' no value ', when using the typeof operator with null, it results as' object ', a mistake that has occurred in the history of JavaScript and remains.

JavaScript

typeof null; / / The result is' object'

The Falsy: null value is considered a falsy value, which means that when converted to a boolean (boolean) value, the result is false.

Differences from undefinite:

The null is configured by the developer to indicate that the variable has no value.

Undefined means a variable has been declared but has not yet been configured or access to a property that does not exist.

Example of use

You may use null to:

Default value defined: requires the variable to start with a known 'empty' value.

JavaScript

Let selectedUser = null; / / The selected user does not have

Clear Reference Value: Remove references from objects to help with memory management.

JavaScript

Let data aObject = {name: "Alice"};

/ /....Use data aObject...

Data aObject = null; / / Clears references when this object is no longer needed.

Comparison

Comparison. Results. Notes.

Null = = undefined true Loose comparison (loose equality) assumes that the two are equal.

Null = = = undefined false A strict comparison (strict equality) assumes that the two have unequal kinds and values.

Null = = = null true -

null = = 0 false null is not equal to 0.

Null > 0 false -

Null > = 0 true When compared mathematically, null is often converted to 0.

Undefined in JavaScript is a value that represents a missing value. It occurs when a variable is declared but has not been defined by default, or when trying to access the property of an object (object) or a member of an array (array) that does not exist.

Important characteristics of undefined

Data Type (Type): The data type of undefined is' undefined '.

JavaScript

typeof undefined; / / The result is' undefined'

Falsy value: undefined is considered a falsy value, which means that when converted to a boolean (boolean) value, the result is false.

Differences from null:

Undefined is automatically the default value when the variable is missing a value (unintended).

Null is a value that the developer intentionally defined to indicate that the variable does not have a valid value or does not point to any object.

An example that causes undefined

Variables declared but without default:

JavaScript

Let name;

console.log (name); / / result: undefined

Access to properties not available in the object:

JavaScript

const user = {id: 101};

console.log (user.email); / / Result: undefined

The non-return function is clear:

JavaScript

greet (name) function {

console.log ('Hello, ${name}!');

/ / This function does not use the return command.

}

Const result = greet ('Alice');

console.log (result); / / result: undefined

Comparison

Comparison. Results. Notes.

Undefined = = null true Loose comparison (loose equality) assumes that the two are equal.

Undefined = = = null false A strict comparison (strict equality) assumes that the two have unequal kinds and values.

undefined = = = undefined true

NaN in JavaScript, short for "Not a Number" (not a number), is a special value in the JavaScript language that represents the result of a mathematical operation that cannot provide a valid numerical value.

Key characteristics of NaN

Data type (Type): Although it means "not a number," the data type of NaN is' number '.

JavaScript

typeof NaN; / / The result is' number'

Falsy value: NaN is considered a falsy value.

Cause of NaN

NaN usually occurs when you try to perform mathematical operations that do not make sense or cannot. Such as:

Dividing zero by zero:

JavaScript

0 / 0; / / Result: NaN

Mathematical operations on non-numeric values:

JavaScript

'Hello '* 5; / / Result: NaN

10 - 'World'; / / Result: NaN

Trying to convert a non-numerical value into a number:

JavaScript

ParseInt ('apple'); / / Result: NaN

Number ('oops'); / / Result: NaN

Comparison and investigation of NaN

The most important thing to remember about NaN is:

NaN is not equal to itself: this is a special feature of it that is different from other values in JavaScript.

JavaScript

NaN = = NaN; / / Result: false

NaN = = = NaN; / / Result: false

So to determine which values are NaN, you can not use the comparative operator (= = or = = =), but the specific function:

The isNaN () function: is a recommended way to determine if a value is NaN.

JavaScript

isNaN (10); / / Result: false (10 as a number)

isNaN (NaN); / / Result: true

isNaN ('Hello'); / / Result: true (because 'Hello' was tried to convert to a number and then got NaN)

The Number.isNaN () function: It is a more rigorous and better way to determine if a value is NaN without trying to convert a non-numeric value into a number (as isNaN () does).

JavaScript

Number.isNaN (NaN); / / Result: true

Number.isNaN ('Hello'); / / Result: false (because 'Hello' is not a direct NaN value.

2025/10/14 Edited to

... Read moreเวลาค้นว่า “undefined แปลว่าอะไร” ใน JavaScript ส่วนใหญ่คือเราเจอค่าประหลาด ๆ ตอน console.log หรือดีบั๊ก API แล้วไม่แน่ใจว่ามัน “ไม่มีค่า” แบบไหนกันแน่ ระหว่าง undefined, null หรือ NaN ซึ่งแต่ละตัวความหมายต่างกันและทำให้บั๊กคนละแบบ 1) undefined แปลว่าอะไร (ในมุมคนเขียนโค้ด) สำหรับฉัน undefined คือ “ยังไม่มีค่า/ระบบยังไม่ได้ให้ค่า” มักเกิดเองอัตโนมัติ เช่น - ประกาศตัวแปรไว้แต่ยังไม่ assign - อ่าน property ที่ไม่มีอยู่ - ฟังก์ชันไม่ได้ return ค่า ตัวอย่างที่เจอบ่อยมากตอนดึงข้อมูล: const user = { id: 1 }; console.log(user.email); // undefined แปลว่า key email ไม่มีอยู่จริง ไม่ได้แปลว่าเป็นค่าว่าง 2) สถานะ null คืออะไร null คือ “ไม่มีค่าโดยเจตนา” คือคนเขียนตั้งใจใส่เพื่อบอกว่า ณ ตอนนี้ไม่มีสิ่งที่ชี้ไป (เช่น ยังไม่เลือกผู้ใช้, ยังไม่มีผลลัพธ์) ฉันชอบใช้ null เป็นค่าเริ่มต้นที่ตั้งใจให้เป็น “ว่างแบบรู้ตัว” เช่น let selectedUser = null; ข้อควรจำที่ทำให้คนงง: typeof null ได้ "object" (เป็นบั๊กเชิงประวัติศาสตร์ของ JavaScript) ดังนั้นถ้าจะเช็ก null ให้ใช้ selectedUser === null ไม่ใช่เช็กด้วย typeof 3) เคส “0 null undefined nan” เกิดจากอะไร เวลาคุณเห็นค่าพวกนี้อยู่ด้วยกัน มักเกิดจากการคำนวณ/แปลงชนิดข้อมูล (type coercion) และค่า falsy - 0 เป็นตัวเลขปกติ แต่เป็น falsy - null เป็น “ตั้งใจว่าง” และเป็น falsy - undefined เป็น “ยังไม่มีค่า” และเป็น falsy - NaN คือผลลัพธ์ที่ “คำนวณออกมาเป็นตัวเลขไม่ได้” (ชนิดยังเป็น number) ตัวอย่างที่ทำให้ NaN โผล่: Number('hello') // NaN 0/0 // NaN 4) เปรียบเทียบให้ไม่พลาด: == vs === เวลาต้องเทียบค่า แนะนำใช้ === เป็นหลัก เพราะไม่เกิดการแปลงชนิดมั่ว ๆ - null == undefined เป็น true (เพราะกฎ loose equality) - null === undefined เป็น false อีกจุดที่ฉันเคยพลาด: เปรียบเทียบเชิงตัวเลข null >= 0 เป็น true แต่ null > 0 เป็น false เพราะตอนเปรียบเทียบเชิงคณิตศาสตร์ null มักถูกแปลงเป็น 0 ทำให้ผลดูแปลก ๆ 5) วิธีเช็ก NaN ที่ควรใช้จริง ห้ามใช้ NaN === NaN (จะได้ false เสมอ) ถ้าต้องการเช็ก “เป็น NaN จริง ๆ” ให้ใช้: Number.isNaN(value) เพราะ isNaN(value) จะพยายามแปลงก่อน ทำให้บางค่าดูเหมือน NaN ทั้งที่ไม่ใช่ NaN โดยตรง สรุปสั้น ๆ: undefined = ยังไม่มีค่า, null = ตั้งใจให้ไม่มีค่า, NaN = ค่าตัวเลขที่ไม่ใช่ตัวเลข (คำนวณพัง) และเวลาเทียบค่าให้ยึด === เป็นหลักเพื่อกันบั๊ก

Related posts

Views of Guatemala
One of the best views on a trip I’ve seen in a while 😍 #guatemala #latinamerica #travelvlogger Guatemala ✈️ Guatemala City/Antigua ✨ Pros: very safe cities and easy to navigate 👀 Cons: aren’t many places to stay 🗺️ Tips: Spanish is more widely spoken so be sure to brush up on y
Natalie’s Travel Diaries

Natalie’s Travel Diaries

9 likes

My mood board 2026✨✨✨🫶🥰
#fashiondesigner #sewingidea #moodboardideas Florida 💌 Brand:Lena undefined
Janina|Fashion Designer

Janina|Fashion Designer

2 likes

#movie #film #jackiechan #fyp #tv
di

di

0 likes

Adept Cosmetics RHNULL Palette 🖤
#makeup #eyeshadow #glitter #Shimmer #beauty
PoshLehr🖤

PoshLehr🖤

7 likes

Adept Cosmetics RHNULL Palette 🖤
#makeup #eyeshadow #Shimmer #glitter #beauty
PoshLehr🖤

PoshLehr🖤

2 likes

Adept Cosmetics RHNULL eyeshadow Palette 🖤
#makeup #eyeshadow #Shimmer #glitter #newmakeup
PoshLehr🖤

PoshLehr🖤

18 likes

A dark desk setup with a laptop displaying code and a book, featuring text overlays 'Reinvent Your Daily Routine w/ These ChatGPT Prompts' and 'All Prompts Included Below'.
A dark desk setup with a laptop showing code and a notebook, detailing 'Prompt 1: Priorities Map' for organizing tasks into a schedule and prioritized list.
A desk with two monitors, one showing a video call, illustrating 'Prompt 2: Spot Energy Leaks' for analyzing routines and suggesting alternatives.
3 ChatGPT Prompts To REINVENT Your Life
Let these 3 prompts to take control of your day, eliminate distractions, and focus on what matters. Copy & Paste ⬇️ ⤷ Prompt 1: Priorities Map Act as a productivity coach. Ask me to list all tasks, meetings, and priorities for today. • Organize them into a detailed hour-by-hour sc
Deft Point Consulting

Deft Point Consulting

73 likes

If you have an undefined G center, you may relate. #humandesign #humandesigntiktok
BrandiYates | Human Design

BrandiYates | Human Design

2 likes

🥹 #kpopdemonhunters #whatitsoundslike
christinesellscarolina

christinesellscarolina

0 likes

Mrs.Null
#gettoknowme South Carolina
Abbiegail Buck

Abbiegail Buck

3 likes

❤️Click my profile link【DS1】 for full video🎬 Eight years, fate still hasn’t let go, and love doesn’t disappear—it just waits to be reclaimed... #DreameShort #shortsdrama #drama # #shortmovie
Drama1556

Drama1556

0 likes

happy May! This months mail is overwhelmingly floral themed. Signups for this edition “A Basket for Two” have closed already, but you can still join and you will receive Junes edition as your first :) #printclub #mailclub #childrensbookillustration #cottagecoreart #watercolorpainting
lunaagem🍄

lunaagem🍄

0 likes

Big Baby - Undefined Series Cute Plush Blind Box Son. You can contact me if you need ☺️👍Pros :✨Outfit Details:
Blg baby blind box

Blg baby blind box

3 likes

Engaging besties alert #creatorprogram #series #tiktokseries #tiktokcreator #tiktokcreatorpilotprogram alright guys! Your nightly opportunity to swoop up the besties that make this app everything that it was designed to be! Swoop them up #engaging #viral #relatable #trending #iwanttogovir
Kass Shaw

Kass Shaw

0 likes

gorgeous 😍
Just a little Dump!! #beautiful
Dixie💫

Dixie💫

0 likes

this and golden✨ absolutely love them! #fyp #kpop #foryoupage #viral #LanguageLearning
Tessa 🐄🌵🤠

Tessa 🐄🌵🤠

0 likes

Being undefined is the romance of women of my gene
Emma

Emma

9 likes

Back to back
#lemon8dairy #huntercallofthwild
Frogg3r12345

Frogg3r12345

0 likes

See more