Hướng dẫn is null == in javascript? - là null == trong javascript?

Giá trị

if (variable === null)
3 đại diện cho sự vắng mặt có chủ ý của bất kỳ giá trị đối tượng nào. Đây là một trong những giá trị nguyên thủy của JavaScript và được coi là giả cho các hoạt động Boolean.

Thử nó

Cú pháp

Sự mô tả

Giá trị

if (variable === null)
3 được viết bằng chữ:
if (variable === null)
3.
if (variable === null)
3 không phải là một định danh cho một thuộc tính của đối tượng toàn cầu, như
if (variable === null)
7 có thể. Thay vào đó,
if (variable === null)
3 thể hiện sự thiếu nhận dạng, chỉ ra rằng một biến chỉ ra không có đối tượng. Trong API,
if (variable === null)
3 thường được truy xuất ở một nơi có thể mong đợi một đối tượng nhưng không có đối tượng nào có liên quan.

// foo does not exist. It is not defined and has never been initialized:
foo; //ReferenceError: foo is not defined

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null

Ví dụ

Sự khác biệt giữa if (variable === null) 3 và if (variable === null) 7

Khi kiểm tra

if (variable === null)
3 hoặc
if (variable === null)
7, hãy cẩn thận với sự khác biệt giữa các toán tử bình đẳng (==) và danh tính (===), vì trước đây thực hiện chuyển đổi loại.

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # sec-null-giá trị
# sec-null-value

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

JavaScript rất linh hoạt liên quan đến việc kiểm tra các giá trị "null". Tôi đoán rằng bạn thực sự đang tìm kiếm các chuỗi trống, trong trường hợp mã đơn giản hơn này sẽ hoạt động:

if(!pass || !cpass || !email || !cemail || !user){

Sẽ kiểm tra các chuỗi trống (

Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
4),
if (variable === null)
3,
if (variable === null)
7,
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
7 và các số
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
8 và
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
9.

Xin lưu ý rằng nếu bạn đặc biệt kiểm tra các số, đó là một sai lầm phổ biến đối với việc bỏ lỡ

Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
8 với phương pháp này và
[pass,cpass,email,cemail,user].some(x=> x===null) 
1 được ưu tiên (hoặc
[pass,cpass,email,cemail,user].some(x=> x===null) 
2 hoặc
[pass,cpass,email,cemail,user].some(x=> x===null) 
3 (mã hack cũng kiểm tra đối với
[pass,cpass,email,cemail,user].some(x=> x===null) 
4)) cho các chức năng trả về
[pass,cpass,email,cemail,user].some(x=> x===null) 
4, ví dụ:
[pass,cpass,email,cemail,user].some(x=> x===null) 
6).

Ryan M ♦

16.8k30 Huy hiệu vàng58 Huy hiệu bạc68 Huy hiệu Đồng30 gold badges58 silver badges68 bronze badges

Đã trả lời ngày 14 tháng 5 năm 2011 lúc 18:20May 14, 2011 at 18:20

7

Để kiểm tra NULL cụ thể, bạn sẽ sử dụng điều này:SPECIFICALLY you would use this:

if (variable === null)

Thử nghiệm này sẽ chỉ vượt qua cho

if (variable === null)
3 và sẽ không vượt qua cho
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
4,
if (variable === null)
7,
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
7,
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
8 hoặc
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
9.ONLY pass for
if (variable === null)
3 and will not pass for
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
4,
if (variable === null)
7,
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
7,
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
8, or
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
9.

Ngoài ra, tôi đã cung cấp kiểm tra tuyệt đối cho từng giá trị "giống như sai" (một giá trị sẽ trả về đúng cho

if ( value === null ){

}
3).

Lưu ý, đối với một số kiểm tra tuyệt đối, bạn sẽ cần thực hiện việc sử dụng

if ( value === null ){

}
4 và
if ( value === null ){

}
5.

Tôi đã tạo một JSfiddle ở đây để hiển thị tất cả các bài kiểm tra riêng lẻ hoạt độngJSFiddle here to show all of the individual tests working

Đây là đầu ra của mỗi lần kiểm tra:

Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number

Như bạn có thể thấy, khó khăn hơn một chút để kiểm tra chống lại

Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
9;

Hướng dẫn is null == in javascript? - là null == trong javascript?

Isherwood

55K16 Huy hiệu vàng108 Huy hiệu bạc150 Huy hiệu Đồng16 gold badges108 silver badges150 bronze badges

Đã trả lời ngày 18 tháng 12 năm 2014 lúc 16:01Dec 18, 2014 at 16:01

Hướng dẫn is null == in javascript? - là null == trong javascript?

WebWandererWebandererWebWanderer

9.6403 Huy hiệu vàng30 Huy hiệu bạc48 Huy hiệu đồng3 gold badges30 silver badges48 bronze badges

8

Chỉ cần thay thế

if ( value === null ){

}
7 bằng
if ( value === null ){

}
8 ở tất cả các nơi.

if ( value === null ){

}
7 là một so sánh bình đẳng lỏng lẻo hoặc trừu tượng

if ( value === null ){

}
8 là một so sánh bình đẳng nghiêm ngặt

Xem bài viết của MDN về so sánh bình đẳng và sự giống nhau để biết thêm chi tiết.

Đã trả lời ngày 14 tháng 5 năm 2011 lúc 18:27May 14, 2011 at 18:27

ic3b3rgic3b3rgic3b3rg

14.3k4 Huy hiệu vàng27 Huy hiệu bạc51 Huy hiệu Đồng4 gold badges27 silver badges51 bronze badges

5

Bạn có thể kiểm tra xem một số giá trị là NULL như sau

[pass,cpass,email,cemail,user].some(x=> x===null) 

Phần thưởng: Tại sao

if ( value === null ){

}
8 rõ ràng hơn
if ( value === null ){

}
7 (nguồn)

a == b

Hướng dẫn is null == in javascript? - là null == trong javascript?

a === b

Hướng dẫn is null == in javascript? - là null == trong javascript?

Đã trả lời ngày 23 tháng 7 năm 2020 lúc 10:15Jul 23, 2020 at 10:15

Hướng dẫn is null == in javascript? - là null == trong javascript?

Kamil Kiełczewskikamil KiełczewskiKamil Kiełczewski

77.4K27 Huy hiệu vàng340 Huy hiệu bạc319 Huy hiệu đồng27 gold badges340 silver badges319 bronze badges

3

Toán tử bình đẳng nghiêm ngặt:-

Chúng tôi có thể kiểm tra NULL bằng

if ( value === null ){

}
8

if ( value === null ){

}

Chỉ bằng cách sử dụng

if( value ) {

}
4

if( value ) {

}

sẽ đánh giá đúng nếu giá trị không:value is not:

  • null
  • chưa xác định
  • Nan
  • Chuỗi trống ("")
  • sai
  • 0

Đã trả lời ngày 5 tháng 7 năm 2016 lúc 11:58Jul 5, 2016 at 11:58

Hướng dẫn is null == in javascript? - là null == trong javascript?

Arshid Kvarshid KVArshid KV

9.3233 huy hiệu vàng33 Huy hiệu bạc36 Huy hiệu đồng3 gold badges33 silver badges36 bronze badges

0

Thoạt nhìn, nó trông giống như một sự đánh đổi đơn giản giữa phạm vi bảo hiểm và sự nghiêm ngặt.between coverage and strictness.

  • if ( value === null ){
    
    }
    
    7 bao gồm nhiều giá trị, có thể xử lý nhiều kịch bản hơn trong ít mã hơn.
  • if ( value === null ){
    
    }
    
    8 là nghiêm ngặt nhất, và điều đó làm cho nó có thể dự đoán được.

Khả năng dự đoán luôn chiến thắng và điều đó dường như tạo ra

if ( value === null ){

}
8 một giải pháp một phù hợp.

Hướng dẫn is null == in javascript? - là null == trong javascript?

Nhưng nó là sai. Mặc dù

if ( value === null ){

}
8 có thể dự đoán được, nhưng nó không phải lúc nào cũng dẫn đến mã có thể dự đoán được, bởi vì nó bỏ qua các kịch bản.wrong. Even though
if ( value === null ){

}
8 is predictable, it does not always result in predictable code, because it overlooks scenarios.

const options = { };
if (options.callback !== null) {
  options.callback();      // error --> callback is undefined.
}

Nói chung

if ( value === null ){

}
7 thực hiện một công việc dễ dự đoán hơn để kiểm tra null:
if ( value === null ){

}
7 does a more predictable job for null checks:

  • Nói chung,

    if (variable === null)
    
    3 và
    if (variable === null)
    
    7 đều có nghĩa là cùng một điều: "Thiếu một cái gì đó". Để dự đoán, bạn cần kiểm tra cả hai giá trị. Và sau đó
    const options = { };
    if (options.callback !== null) {
      options.callback();      // error --> callback is undefined.
    }
    
    2 thực hiện một công việc hoàn hảo, bởi vì nó bao gồm chính xác 2 giá trị đó. (tức là
    const options = { };
    if (options.callback !== null) {
      options.callback();      // error --> callback is undefined.
    }
    
    2 tương đương với
    const options = { };
    if (options.callback !== null) {
      options.callback();      // error --> callback is undefined.
    }
    
    4)

  • Trong những trường hợp đặc biệt, bạn muốn có một sự khác biệt rõ ràng giữa

    if (variable === null)
    
    3 và
    if (variable === null)
    
    7. Và trong những trường hợp đó, bạn tốt hơn với
    const options = { };
    if (options.callback !== null) {
      options.callback();      // error --> callback is undefined.
    }
    
    7 hoặc
    const options = { };
    if (options.callback !== null) {
      options.callback();      // error --> callback is undefined.
    }
    
    8 nghiêm ngặt. (ví dụ: sự khác biệt giữa thiếu/bỏ qua/bỏ qua và trống/xóa/xóa.) Nhưng nó rất hiếm.do want a clear distinction between
    if (variable === null)
    
    3 and
    if (variable === null)
    
    7. And in those cases you're better of with a strict
    const options = { };
    if (options.callback !== null) {
      options.callback();      // error --> callback is undefined.
    }
    
    7 or
    const options = { };
    if (options.callback !== null) {
      options.callback();      // error --> callback is undefined.
    }
    
    8. (e.g. a distinction between missing/ignore/skip and empty/clear/remove.) But it is rare.

Nó không chỉ hiếm, nó là một cái gì đó để tránh. Bạn không thể lưu trữ

if (variable === null)
7 trong cơ sở dữ liệu truyền thống. Và bạn không nên dựa vào các giá trị
if (variable === null)
7 trong các thiết kế API của mình, vì lý do khả năng tương tác. Nhưng ngay cả khi bạn không phân biệt được, bạn không thể cho rằng
if (variable === null)
7 sẽ không xảy ra. Mọi người xung quanh chúng ta gián tiếp thực hiện các hành động khái quát hóa ____ 43/________ 47 (đó là lý do tại sao các câu hỏi như thế này được đóng lại là "ý kiến".).you can't assume that
if (variable === null)
7 won't happen.
People all around us indirectly take actions that generalize
if (variable === null)
3/
if (variable === null)
7 (which is why questions like this are closed as "opinionated".).

Vì vậy, để trở lại câu hỏi của bạn. Không có gì sai khi sử dụng

const options = { };
if (options.callback !== null) {
  options.callback();      // error --> callback is undefined.
}
2. Nó làm chính xác những gì nó nên làm.

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
0

Đã trả lời ngày 30 tháng 12 năm 2020 lúc 18:03Dec 30, 2020 at 18:03

Hướng dẫn is null == in javascript? - là null == trong javascript?

BVDBBVDBbvdb

21.1k10 Huy hiệu vàng100 Huy hiệu bạc117 Huy hiệu đồng10 gold badges100 silver badges117 bronze badges

Cải thiện câu trả lời được chấp nhận bằng cách kiểm tra rõ ràng

if (variable === null)
3 nhưng với cú pháp đơn giản hóa:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
1

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
2

Đã trả lời ngày 14 tháng 3 năm 2018 lúc 3:45Mar 14, 2018 at 3:45

Hướng dẫn is null == in javascript? - là null == trong javascript?

DeekshithDeekshithDeekshith

1.44414 huy hiệu bạc15 huy hiệu đồng14 silver badges15 bronze badges

Đầu tiên, bạn có một câu lệnh trả lại mà không có cơ thể chức năng. Rất có thể điều đó sẽ ném một lỗi.

Một cách sạch hơn để thực hiện kiểm tra của bạn sẽ chỉ đơn giản là sử dụng! nhà điều hành:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
3

Hướng dẫn is null == in javascript? - là null == trong javascript?

Đã trả lời ngày 14 tháng 5 năm 2011 lúc 18:20May 14, 2011 at 18:20

Joey C.Joey C.Joey C.

2.0392 Huy hiệu vàng16 Huy hiệu bạc14 Huy hiệu đồng2 gold badges16 silver badges14 bronze badges

1

Bạn có thể sử dụng thử Catch Cuối cùng

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
4

Bạn cũng có thể

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
06 lỗi của riêng bạn. Xem điều này.

Hướng dẫn is null == in javascript? - là null == trong javascript?

Đã trả lời ngày 14 tháng 5 năm 2011 lúc 18:25May 14, 2011 at 18:25

DrstrangeledrstrangeloveDrStrangeLove

10,8K16 Huy hiệu vàng58 Huy hiệu bạc70 Huy hiệu Đồng16 gold badges58 silver badges70 bronze badges

2

Trong JavaScript, không có chuỗi nào bằng

if (variable === null)
3.

Có thể bạn mong đợi

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
08 là đúng khi
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
09 là một chuỗi trống bởi vì bạn biết rằng toán tử bình đẳng lỏng lẻo
if ( value === null ){

}
7 thực hiện một số loại ép buộc loại.

Ví dụ, biểu thức này là đúng:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
5

Ngược lại, toán tử bình đẳng nghiêm ngặt

if ( value === null ){

}
8 nói rằng điều này là sai:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
6

Cho rằng

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
12 và
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
8 bằng một cách lỏng lẻo, bạn có thể phỏng đoán một cách hợp lý rằng
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
12 và
if (variable === null)
3 bằng nhau một cách lỏng lẻo. Tuy nhiên, họ không.

Biểu thức này là sai:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
7

Kết quả của việc so sánh bất kỳ chuỗi nào với

if (variable === null)
3 là sai. Do đó,
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
08 và tất cả các bài kiểm tra khác của bạn luôn sai và người dùng không bao giờ nhận được cảnh báo.

Để sửa mã của bạn, hãy so sánh từng giá trị với chuỗi trống:

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
8

Nếu bạn chắc chắn rằng

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
09 là một chuỗi,
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
19 cũng sẽ hoạt động vì chỉ một chuỗi trống được lỏng lẻo bằng chuỗi trống. Mặt khác, một số chuyên gia nói rằng đó là một thông lệ tốt để luôn sử dụng sự bình đẳng nghiêm ngặt trong JavaScript trừ khi bạn đặc biệt muốn thực hiện sự ép buộc loại mà người vận hành bình đẳng lỏng lẻo thực hiện.

Nếu bạn muốn biết những cặp giá trị nào bằng nhau, hãy xem bảng "so sánh giống nhau" trong bài viết của Mozilla về chủ đề này.

Đã trả lời ngày 27 tháng 8 năm 2015 lúc 23:28Aug 27, 2015 at 23:28

Hướng dẫn is null == in javascript? - là null == trong javascript?

Michael Laszlomichael LaszloMichael Laszlo

11.7K2 Huy hiệu vàng27 Huy hiệu bạc46 Huy hiệu đồng2 gold badges27 silver badges46 bronze badges

Để kiểm tra xem không xác định và NULL trong JavaScript, bạn chỉ cần viết như sau:undefined and null in javascript you need just to write the following :

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
9

Sunny Patel

7.5942 Huy hiệu vàng32 Huy hiệu bạc44 Huy hiệu đồng2 gold badges32 silver badges44 bronze badges

Đã trả lời ngày 25 tháng 2 năm 2015 lúc 10:48Feb 25, 2015 at 10:48

Hướng dẫn is null == in javascript? - là null == trong javascript?

1

Trên thực tế, tôi nghĩ rằng bạn có thể cần sử dụng

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
20 bởi vì nếu bạn sử dụng
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
21, bạn cũng có thể lọc 0 hoặc các giá trị sai.

Hãy xem xét hai chức năng này:

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
0

Trong tình huống của tôi, tôi chỉ cần kiểm tra xem giá trị là null và không xác định và tôi không muốn lọc các giá trị

Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
8 hoặc
Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
7 hoặc
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
12. Vì vậy, tôi đã sử dụng thử nghiệm thứ hai, nhưng bạn có thể cần phải lọc chúng cũng có thể khiến bạn sử dụng thử nghiệm đầu tiên.

Đã trả lời ngày 4 tháng 11 năm 2018 lúc 14:52Nov 4, 2018 at 14:52

Hướng dẫn is null == in javascript? - là null == trong javascript?

Naeem Baghinaireem BaghiNaeem Baghi

8032 Huy hiệu vàng13 Huy hiệu bạc27 Huy hiệu đồng2 gold badges13 silver badges27 bronze badges

2

Đây là một nhận xét về giải pháp của Webwanderer liên quan đến việc kiểm tra NAN (tôi chưa có đủ đại diện để để lại nhận xét chính thức). Giải pháp đọc là

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
1

Nhưng điều này sẽ thất bại đối với các số hợp lý sẽ làm tròn đến

Null Test:

if (variable === null)

- variable = ""; (false) typeof variable = string

- variable = null; (true) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Empty String Test:

if (variable === '')

- variable = ''; (true) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number




Undefined Test:

if (typeof variable == "undefined")

-- or --

if (variable === undefined)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (true) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



False Test:

if (variable === false)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (true) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (false) typeof variable = number



Zero Test:

if (variable === 0)

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (true) typeof variable = number

- variable = NaN; (false) typeof variable = number



NaN Test:

if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)

-- or --

if (isNaN(variable))

- variable = ''; (false) typeof variable = string

- variable = null; (false) typeof variable = object

- variable = undefined; (false) typeof variable = undefined

- variable = false; (false) typeof variable = boolean

- variable = 0; (false) typeof variable = number

- variable = NaN; (true) typeof variable = number
8, chẳng hạn như
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
26. Một bài kiểm tra tốt hơn sẽ là:

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
2

Đã trả lời ngày 18 tháng 2 năm 2015 lúc 19:10Feb 18, 2015 at 19:10

Hướng dẫn is null == in javascript? - là null == trong javascript?

GabrielgabrielGabriel

5804 Huy hiệu bạc14 Huy hiệu đồng4 silver badges14 bronze badges

2

Bạn có thể sử dụng mô -đun lodash để kiểm tra giá trị là null hoặc không xác định

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
3

Liên kết tham khảo: https://lodash.com/docs/#isnil

Đã trả lời ngày 19 tháng 5 năm 2019 lúc 6:34May 19, 2019 at 6:34

AFAIK trong JavaScript Khi một biến được khai báo nhưng không được gán giá trị, loại của nó là

if (variable === null)
7. Vì vậy, chúng tôi có thể kiểm tra biến ngay cả khi nó sẽ là một
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
28 giữ một số trường hợp thay cho giá trị.JAVASCRIPT when a variable is declared but has not assigned value, its type is
if (variable === null)
7. so we can check variable even if it would be an
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
28 holding some instance in place of value.

Tạo một phương thức trợ giúp để kiểm tra độ vô tính trả về

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
29 và sử dụng nó trong API của bạn.

Chức năng của người trợ giúp để kiểm tra xem biến có trống không:

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
4

Thử-catch Call đặc biệt API:

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
5

Một số trường hợp kiểm tra:

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
6

Đã trả lời ngày 5 tháng 10 năm 2015 lúc 14:46Oct 5, 2015 at 14:46

Hướng dẫn is null == in javascript? - là null == trong javascript?

Kaleem ullahkaleem ullahKaleem Ullah

6.4913 huy hiệu vàng39 Huy hiệu bạc47 Huy hiệu đồng3 gold badges39 silver badges47 bronze badges

2

Tôi đã tìm thấy một cách khác để kiểm tra nếu giá trị là NULL:

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
7

if (variable === null)
3 hoạt động như một
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
31 và
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
28 cùng một lúc. So sánh
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
33 hoặc
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
34 kết quả trong
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
29. So sánh
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
36 hoặc
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
37 hoặc
// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
38 sẽ dẫn đến sai. Nhưng vì
if (variable === null)
3 cũng là một đối tượng chúng ta có thể phát hiện nó dưới dạng null.

Tôi đã tạo ra một chức năng phức tạp hơn, phù thủynatureof witch will do better than typeof and can be told what types to include or keep grouped

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
8

Đã trả lời ngày 8 tháng 3 năm 2019 lúc 11:10Mar 8, 2019 at 11:10

Tôi đã làm cho chức năng rất đơn giản này hoạt động kỳ diệu:

typeof null          // "object" (not "null" for legacy reasons)
typeof undefined     // "undefined"
null === undefined   // false
null  == undefined   // true
null === null        // true
null  == null        // true
!null                // true
isNaN(1 + null)      // false
isNaN(1 + undefined) // true
9

Tuyến đường là bất kỳ chuỗi giá trị nào có thể nổ tung. Tôi sử dụng nó cho jQuery/Cheerio và các đối tượng và như vậy.

Ví dụ 1: Một đối tượng đơn giản như

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
40 này.

Nhưng nó có thể là một đối tượng rất lớn mà chúng ta thậm chí chưa thực hiện. Vì vậy, tôi vượt qua nó:

if(!pass || !cpass || !email || !cemail || !user){
0

Tất nhiên nếu bạn thích bạn có thể sử dụng

if (variable === null)
3 hoặc ________ 142 ... bất cứ điều gì phù hợp với nhu cầu của bạn.

Thông thường một truy vấn Dom hoặc bộ chọn jQuery có thể gây ra lỗi nếu không tìm thấy. Nhưng sử dụng một cái gì đó như:

if(!pass || !cpass || !email || !cemail || !user){
1

Đã trả lời ngày 12 tháng 3 năm 2019 lúc 6:56Mar 12, 2019 at 6:56

Neithan Maxneithan MaxNeithan Max

9.8965 Huy hiệu vàng35 Huy hiệu bạc57 Huy hiệu Đồng5 gold badges35 silver badges57 bronze badges

Điều gì về kiểm tra tùy chọn với toán tử?

Ví dụ:

if(!pass || !cpass || !email || !cemail || !user){
2

Đã trả lời ngày 20 tháng 12 năm 2021 lúc 22:37Dec 20, 2021 at 22:37

PitpitPit

3351 Huy hiệu bạc11 Huy hiệu đồng1 silver badge11 bronze badges

Giải pháp đơn giản cho các giá trị trống:

if(!pass || !cpass || !email || !cemail || !user){
3

Đã trả lời ngày 13 tháng 6 lúc 4:21Jun 13 at 4:21

Amr Omaramr OmarAmr Omar

3395 Huy hiệu bạc9 Huy hiệu Đồng5 silver badges9 bronze badges

Phương pháp '

// foo is known to exist now but it has no type or value:
const foo = null;
foo; //null
43' có thể được sử dụng để xác định xem hai giá trị có cùng một giá trị hay không. Vì vậy, bạn có thể sử dụng nó để kiểm tra xem đối tượng có null hay không.

Kiểm tra các giá trị null

if(!pass || !cpass || !email || !cemail || !user){
4
if(!pass || !cpass || !email || !cemail || !user){
5

Kiểm tra các giá trị không xác định

if(!pass || !cpass || !email || !cemail || !user){
6
if(!pass || !cpass || !email || !cemail || !user){
7

Nếu bạn muốn kiểm tra cả không xác định và null, hãy sử dụng điều này.

if(!pass || !cpass || !email || !cemail || !user){
8
if(!pass || !cpass || !email || !cemail || !user){
9

Kiểm tra chúng: https://onecompiler.com/javascript/3ymdqd34v

Mozilla Giải thích: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/object/is

Đã trả lời ngày 29 tháng 10 lúc 16:39Oct 29 at 16:39

Hướng dẫn is null == in javascript? - là null == trong javascript?

DxtxdxtxDxTx

2.7892 Huy hiệu vàng19 Huy hiệu bạc33 Huy hiệu đồng2 gold badges19 silver badges33 bronze badges

Thử cái này:

if (variable === null)
0

THX-1138

20.8K26 Huy hiệu vàng94 Huy hiệu bạc154 Huy hiệu đồng26 gold badges94 silver badges154 bronze badges

Đã trả lời ngày 3 tháng 11 năm 2015 lúc 14:23Nov 3, 2015 at 14:23

2

Điều này sẽ không hoạt động trong trường hợp các giá trị Boolean đến từ DB cho Ex:

if (variable === null)
1

Đã trả lời ngày 23 tháng 5 năm 2016 lúc 6:48May 23, 2016 at 6:48

Hướng dẫn is null == in javascript? - là null == trong javascript?

CodieecodieeCodiee

2.9112 Huy hiệu vàng16 Huy hiệu bạc18 Huy hiệu đồng2 gold badges16 silver badges18 bronze badges

Kiểm tra các điều kiện lỗi:

if (variable === null)
2

Đã trả lời ngày 5 tháng 12 năm 2019 lúc 10:27Dec 5, 2019 at 10:27

Hướng dẫn is null == in javascript? - là null == trong javascript?

P-SP-SP-S

Phù vàng 3.5861 Huy hiệu vàng27 Huy hiệu đồng1 gold badge27 silver badges25 bronze badges

Null == Giá trị thực trong JavaScript?

Trong JavaScript, một giá trị sự thật là một giá trị được coi là đúng khi gặp phải trong bối cảnh Boolean. Tất cả các giá trị là sự thật trừ khi chúng được định nghĩa là giả mạo. Đó là, tất cả các giá trị là sự thật ngoại trừ sai, 0, -0, 0n, "", null, không xác định và nan.all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN .

Null == không xác định trong js?

Nó có nghĩa là NULL bằng với không xác định nhưng không giống nhau.Khi chúng tôi xác định một biến để không xác định thì chúng tôi đang cố gắng truyền đạt rằng biến không tồn tại.Khi chúng tôi xác định một biến thành NULL thì chúng tôi đang cố gắng truyền đạt rằng biến trống.null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

Null == true?

Không xác định là một loại nguyên thủy của một biến đánh giá Falsy, có một loại () không xác định và đại diện cho một biến được khai báo nhưng thiếu một giá trị ban đầu.null == đánh giá không xác định là đúng vì chúng bằng nhau một cách lỏng lẻo.null === đánh giá không xác định là sai vì trên thực tế, chúng không bằng nhau.null == undefined evaluates as true because they are loosely equal. null === undefined evaluates as false because they are not, in fact, equal.

Tại sao null == 0 Sai?

So sánh chuyển đổi null thành một số, coi nó là 0.Đó là lý do tại sao (3) null> = 0 là đúng và (1) null> 0 là sai.Mặt khác, kiểm tra bình đẳng == cho không xác định và null được xác định sao cho không có bất kỳ chuyển đổi nào, chúng bằng nhau và không bằng bất cứ thứ gì khác.Đó là lý do tại sao (2) null == 0 là sai.the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don't equal anything else. That's why (2) null == 0 is false.