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 [
if [ value === null ]{
}
0], if [ value === null ]{
}
1, if [ value === null ]{
}
2, if [ value === null ]{
}
3 và các số if [ value === null ]{
}
4 và if [ value === null ]{
}
5.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ỡ
if [ value === null ]{
}
4 với phương pháp này và if [ value === null ]{
}
7 được ưu tiên [hoặc if [ value === null ]{
}
8 hoặc if [ value === null ]{
}
9 [mã hack cũng kiểm tra đối với if[ value ] {
}
0]] cho các chức năng trả về if[ value ] {
}
0, ví dụ: if[ value ] {
}
2].
Ryan M ♦♦
16.7k30 Huy hiệu vàng57 Huy hiệu bạc67 Huy hiệu Đồng30 gold badges57 silver badges67 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 [ value === null ]{
}
1 và sẽ không vượt qua cho if [ value === null ]{
}
0, if [ value === null ]{
}
2, if [ value === null ]{
}
3, if [ value === null ]{
}
4 hoặc if [ value === null ]{
}
5.ONLY pass for if [ value === null ]{
}
1 and will not pass for if [ value === null ]{
}
0, if [ value === null ]{
}
2, if [ value === null ]{
}
3, if [ value === null ]{
}
4, or if [ value === null ]{
}
5.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 ] {
}
9].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
const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
0 và const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
1.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
if [ value === null ]{
}
5;
Isherwood
54,8K16 Huy hiệu vàng108 Huy hiệu bạc149 Huy hiệu đồng16 gold badges108 silver badges149 bronze badges
Đã trả lời ngày 18 tháng 12 năm 2014 lúc 16:01Dec 18, 2014 at 16:01
WebWandererWebandererWebWanderer
9.6003 huy hiệu vàng30 huy hiệu bạc47 Huy hiệu đồng3 gold badges30 silver badges47 bronze badges
8
Chỉ cần thay thế
const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
3 bằng const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
4 ở tất cả các nơi.const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
3 là một so sánh bình đẳng lỏng lẻo hoặc trừu tượngconst options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
4 là một so sánh bình đẳng nghiêm ngặtXem 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
const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
4 rõ ràng hơn const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
3 [nguồn]a == b
a === b
Đã trả lời ngày 23 tháng 7 năm 2020 lúc 10:15Jul 23, 2020 at 10:15
Kamil Kiełczewskikamil KiełczewskiKamil Kiełczewski
76.8K26 Huy hiệu vàng339 Huy hiệu bạc316 Huy hiệu Đồng26 gold badges339 silver badges316 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
const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
4if [ value === null ]{
}
Chỉ bằng cách sử dụng
// FIX 1 --> yes === is very explicit
const options = { };
if [options.callback !== null &&
options.callback !== undefined] {
options.callback[];
}
// FIX 2 --> but == covers both
const options = { };
if [options.callback != null] {
options.callback[];
}
// FIX 3 --> optional chaining also covers both.
const options = { };
options.callback?.[];
0if[ 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
Arshid Kvarshid KVArshid KV
9.2833 huy hiệu vàng33 Huy hiệu bạc35 Huy hiệu đồng3 gold badges33 silver badges35 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.
3 bao gồm nhiều giá trị, có thể xử lý nhiều kịch bản hơn trong ít mã hơn.const options = { }; if [options.callback !== null] { options.callback[]; // error --> callback is undefined. }
4 là nghiêm ngặt nhất, và điều đó làm cho nó có thể dự đoán được.const options = { }; if [options.callback !== null] { options.callback[]; // error --> callback is undefined. }
Khả năng dự đoán luôn chiến thắng và điều đó dường như tạo ra một giải pháp một phù hợp.
Nhưng nó là sai. Mặc dù
const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
4 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
const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
4 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
3 thực hiện một công việc dễ dự đoán hơn để kiểm tra null: const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
3 does a more predictable job for null checks:Nói chung,
1 vàif [ value === null ]{ }
2 đề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 đóif [ value === null ]{ }
8 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à// FIX 1 --> yes === is very explicit const options = { }; if [options.callback !== null && options.callback !== undefined] { options.callback[]; } // FIX 2 --> but == covers both const options = { }; if [options.callback != null] { options.callback[]; } // FIX 3 --> optional chaining also covers both. const options = { }; options.callback?.[];
8 tương đương với// FIX 1 --> yes === is very explicit const options = { }; if [options.callback !== null && options.callback !== undefined] { options.callback[]; } // FIX 2 --> but == covers both const options = { }; if [options.callback != null] { options.callback[]; } // FIX 3 --> optional chaining also covers both. const options = { }; options.callback?.[];
0]if [[pass, cpass, email, cemail, user].every[x=>x!==null]] { // your code here ... }
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
1 vàif [ value === null ]{ }
2. Và trong những trường hợp đó, bạn tốt hơn vớiif [ value === null ]{ }
3 hoặcif [[pass, cpass, email, cemail, user].every[x=>x!==null]] { // your code here ... }
4 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 betweenif [[pass, cpass, email, cemail, user].every[x=>x!==null]] { // your code here ... }
1 andif [ value === null ]{ }
2. And in those cases you're better of with a strictif [ value === null ]{ }
3 orif [[pass, cpass, email, cemail, user].every[x=>x!==null]] { // your code here ... }
4. [e.g. a distinction between missing/ignore/skip and empty/clear/remove.] But it is rare.if [[pass, cpass, email, cemail, user].every[x=>x!==null]] { // your code here ... }
Nó không chỉ hiếm, nó là một cái gì đó để tránh. Bạn không thể lưu trữ
if [ value === null ]{
}
2 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 [ value === null ]{
}
2 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 [ value === null ]{
}
2 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 ____ 41/________ 42 [đó 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 [ value === null ]{
}
2 won't happen. People all around us indirectly take actions that generalize if [ value === null ]{
}
1/if [ value === null ]{
}
2 [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
// FIX 1 --> yes === is very explicit
const options = { };
if [options.callback !== null &&
options.callback !== undefined] {
options.callback[];
}
// FIX 2 --> but == covers both
const options = { };
if [options.callback != null] {
options.callback[];
}
// FIX 3 --> optional chaining also covers both.
const options = { };
options.callback?.[];
8. Nó làm chính xác những gì nó nên làm.// FIX 1 --> yes === is very explicit
const options = { };
if [options.callback !== null &&
options.callback !== undefined] {
options.callback[];
}
// FIX 2 --> but == covers both
const options = { };
if [options.callback != null] {
options.callback[];
}
// FIX 3 --> optional chaining also covers both.
const options = { };
options.callback?.[];
Đã trả lời ngày 30 tháng 12 năm 2020 lúc 18:03Dec 30, 2020 at 18:03
BVDBBVDBbvdb
21K10 Huy hiệu vàng100 Huy hiệu bạc116 Huy hiệu đồng10 gold badges100 silver badges116 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 [ value === null ]{
}
1 nhưng với cú pháp đơn giản hóa:if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
}
// Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
Đã trả lời ngày 14 tháng 3 năm 2018 lúc 3:45Mar 14, 2018 at 3:45
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:
if [variable === null]
0
Đã 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.0292 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
if [variable === null]
1Bạn cũng có thể
// Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
2 lỗi của riêng bạn. Xem điều này.
Đã 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 [ value === null ]{
}
1.Có thể bạn mong đợi
// Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
4 là đúng khi // Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
5 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 const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
3 thực hiện một số loại ép buộc loại.Ví dụ, biểu thức này là đúng:
if [variable === null]
2Ngược lại, toán tử bình đẳng nghiêm ngặt
const options = { };
if [options.callback !== null] {
options.callback[]; // error --> callback is undefined.
}
4 nói rằng điều này là sai:if [variable === null]
3Cho rằng
// Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
8 và if [ value === null ]{
}
4 bằng nhau 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 // Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
8 và if [ value === null ]{
}
1 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:
if [variable === null]
4Kết quả của việc so sánh bất kỳ chuỗi nào với
if [ value === null ]{
}
1 là sai. Do đó, // Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
4 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:
if [variable === null]
5Nếu bạn chắc chắn rằng
// Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
5 là một chuỗi, if [variable === null]
05 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
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 :
if [variable === null]
6
Sunny Patel
7.5842 Huy hiệu vàng32 Huy hiệu bạc42 Huy hiệu đồng2 gold badges32 silver badges42 bronze badges
Đã trả lời ngày 25 tháng 2 năm 2015 lúc 10:48Feb 25, 2015 at 10:48
1
Trên thực tế, tôi nghĩ rằng bạn có thể cần sử dụng
if [variable === null]
06 bởi vì nếu bạn sử dụng if [variable === null]
07, 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:
if [variable === null]
7Trong 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ị
if [ value === null ]{
}
4 hoặc if [ value === null ]{
}
3 hoặc // Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test
if [[pass, cpass, email, cemail, user].every[x=>x!==null]] {
// your code here ...
console.log ["Yayy! None of them are null"];
} else {
console.log ["Oops! At-lease one of them is null"];
}
8. 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
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à
if [variable === null]
8Như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
if [ value === null ]{
}
4, chẳng hạn như if [variable === null]
12. Một bài kiểm tra tốt hơn sẽ là:if [variable === null]
9Đã trả lời ngày 18 tháng 2 năm 2015 lúc 19:10Feb 18, 2015 at 19:10
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
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
0Liên kết tham khảo: //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 [ value === null ]{
}
2. Vì vậy, chúng tôi có thể kiểm tra biến ngay cả khi nó sẽ là một if [variable === null]
14 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 [ value === null ]{
}
2. so we can check variable even if it would be an
if [variable === null]
14 holding some instance in place of value.Tạo một phương thức trợ giúp để kiểm tra vô hiệu trả về
if [variable === null]
15 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:
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
1Thử-catch Call đặc biệt API:
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
2Một số trường hợp 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
3
Đã trả lời ngày 5 tháng 10 năm 2015 lúc 14:46Oct 5, 2015 at 14:46
Kaleem ullahkaleem ullahKaleem Ullah
6.4613 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:
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
4if [ value === null ]{
}
1 hoạt động như một if [variable === null]
17 và if [variable === null]
14 cùng một lúc. So sánh kết quả if [variable === null]
19 hoặc if [variable === null]
20 trong if [variable === null]
15. So sánh if [variable === null]
22 hoặc if [variable === null]
23 hoặc if [variable === null]
24 sẽ dẫn đến sai. Nhưng vì if [ value === null ]{
}
1 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
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
5Đã 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:
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
6Tuyế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ư
if [variable === null]
26 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ó:
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
7Tất nhiên nếu bạn thích bạn có thể sử dụng
if [ value === null ]{
}
1 hoặc ________ 128 ... 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ư:
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Đã 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.8265 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ụ:
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Đã trả lời ngày 20 tháng 12 năm 2021 lúc 22:37Dec 20, 2021 at 22:37
PitpitPit
3291 Huy hiệu bạc10 Huy hiệu đồng1 silver badge10 bronze badges
Giải pháp đơn giản cho các giá trị trống:
[pass,cpass,email,cemail,user].some[x=> x===null]
0Đã 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
Thử cái này:
[pass,cpass,email,cemail,user].some[x=> x===null]
1
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:
[pass,cpass,email,cemail,user].some[x=> x===null]
2Đã trả lời ngày 23 tháng 5 năm 2016 lúc 6:48May 23, 2016 at 6:48
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:
[pass,cpass,email,cemail,user].some[x=> x===null]
3Đã trả lời ngày 5 tháng 12 năm 2019 lúc 10:27Dec 5, 2019 at 10:27
P-SP-SP-S
3,5661 Huy hiệu vàng26 Huy hiệu bạc25 Huy hiệu đồng1 gold badge26 silver badges25 bronze badges
Phương pháp '
if [variable === null]
29' 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
[pass,cpass,email,cemail,user].some[x=> x===null]
4[pass,cpass,email,cemail,user].some[x=> x===null]
5Kiểm tra các giá trị không xác định
[pass,cpass,email,cemail,user].some[x=> x===null]
6[pass,cpass,email,cemail,user].some[x=> x===null]
7Nế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.
[pass,cpass,email,cemail,user].some[x=> x===null]
8[pass,cpass,email,cemail,user].some[x=> x===null]
9Kiểm tra chúng: //onecompiler.com/javascript/3ymdqd34v
Mozilla Giải thích: //developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/object/is
Đã trả lời 2 giờ trước2 hours ago
DxtxdxtxDxTx
2.7692 Huy hiệu vàng19 Huy hiệu bạc32 Huy hiệu Đồng2 gold badges19 silver badges32 bronze badges