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 = ค่าตัวเลขที่ไม่ใช่ตัวเลข (คำนวณพัง) และเวลาเทียบค่าให้ยึด === เป็นหลักเพื่อกันบั๊ก