Hướng dẫn javascript null check - kiểm tra javascript null

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:

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

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

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    4),
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5,
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6,
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    7 và các số
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    8 và
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    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ỡ

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

    Hướng dẫn javascript null check - kiểm tra javascript null

    Ryan M ♦

    16.6K30 Huy hiệu vàng56 Huy hiệu bạc65 Huy hiệu Đồng30 gold badges56 silver badges65 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

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5 và sẽ không vượt qua cho
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    4,
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6,
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    7,
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    8 hoặc
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    9.ONLY pass for
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5 and will not pass for
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    4,
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6,
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    7,
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    8, or
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    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 ) {
    
    }
    
    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 ) {
    
    }
    
    4 và
    if( value ) {
    
    }
    
    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

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

    Hướng dẫn javascript null check - kiểm tra javascript null

    Isherwood

    54.5K15 Huy hiệu vàng106 Huy hiệu bạc147 Huy hiệu đồng15 gold badges106 silver badges147 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 javascript null check - kiểm tra javascript null

    WebWandererWebandererWebWanderer

    9.5703 huy hiệu vàng29 Huy hiệu bạc46 Huy hiệu đồng3 gold badges29 silver badges46 bronze badges

    8

    Chỉ cần thay thế

    if( value ) {
    
    }
    
    7 bằng
    if( value ) {
    
    }
    
    8 ở tất cả các nơi.

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

    if( value ) {
    
    }
    
    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àng26 Huy hiệu bạc 50 Huy hiệu Đồng4 gold badges26 silver badges50 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 ) {
    
    }
    
    8 rõ ràng hơn
    if( value ) {
    
    }
    
    7 (nguồn)

    a == b

    Hướng dẫn javascript null check - kiểm tra javascript null

    a === b

    Hướng dẫn javascript null check - kiểm tra javascript null

    Đã 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 javascript null check - kiểm tra javascript null

    Kamil Kiełczewskikamil KiełczewskiKamil Kiełczewski

    76.2K26 Huy hiệu vàng337 Huy hiệu bạc315 Huy hiệu Đồng26 gold badges337 silver badges315 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 ) {
    
    }
    
    8

    if ( value === null ){
    
    }
    

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

    const options = { };
    if (options.callback !== null) {
      options.callback();      // error --> callback is undefined.
    }
    
    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 javascript null check - kiểm tra javascript null

    Arshid Kvarshid KVArshid KV

    9.2733 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.

    • if( value ) {
      
      }
      
      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 ) {
      
      }
      
      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 ) {
    
    }
    
    8 một giải pháp một phù hợp.

    Hướng dẫn javascript null check - kiểm tra javascript null

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

    if( value ) {
    
    }
    
    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 ) {
    
    }
    
    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 ) {
    
    }
    
    7 thực hiện một công việc dễ dự đoán hơn để kiểm tra null:
    if( value ) {
    
    }
    
    7 does a more predictable job for null checks:

    • Nói chung,

      [pass,cpass,email,cemail,user].some(x=> x===null) 
      
      5 và
      [pass,cpass,email,cemail,user].some(x=> x===null) 
      
      6 đề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 đó
      // 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?.();
      
      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à
      // 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?.();
      
      2 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?.();
      
      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

      [pass,cpass,email,cemail,user].some(x=> x===null) 
      
      5 và
      [pass,cpass,email,cemail,user].some(x=> x===null) 
      
      6. Và trong những trường hợp đó, bạn tốt hơn 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?.();
      
      7 hoặc
      // 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 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
      [pass,cpass,email,cemail,user].some(x=> x===null) 
      
      5 and
      [pass,cpass,email,cemail,user].some(x=> x===null) 
      
      6. And in those cases you're better of with a strict
      // 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?.();
      
      7 or
      // 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. (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ữ

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6 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ị
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6 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
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6 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 ____ 35/________ 36 (đó là lý do tại sao các câu hỏi như thế này được đóng lại là "quan điểm".).you can't assume that
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6 won't happen.
    People all around us indirectly take actions that generalize
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5/
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6 (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?.();
    
    2. 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

    Hướng dẫn javascript null check - kiểm tra javascript null

    BVDBBVDBbvdb

    20.8k10 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

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5 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

    Hướng dẫn javascript null check - kiểm tra javascript null

    DeekshithDeekshithDeekshith

    1.43414 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

    Hướng dẫn javascript null check - kiểm tra javascript null

    Đã 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)
    
    1

    Bạn cũng có thể

    if ([pass, cpass, email, cemail, user].every(x=>x!==null)) {
        // your code here ...
    }
    
    6 lỗi của riêng bạn. Xem điều này.

    Hướng dẫn javascript null check - kiểm tra javascript null

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

    DrstrangeledrstrangeloveDrStrangeLove

    10,7K16 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

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

    Có thể bạn mong đợi

    if ([pass, cpass, email, cemail, user].every(x=>x!==null)) {
        // your code here ...
    }
    
    8 là đúng khi
    if ([pass, cpass, email, cemail, user].every(x=>x!==null)) {
        // your code here ...
    }
    
    9 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 ) {
    
    }
    
    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:

    if (variable === null)
    
    2

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

    if( value ) {
    
    }
    
    8 nói rằng điều này là sai:

    if (variable === null)
    
    3

    Cho 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");
    }
    2 và
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    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
    // 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 và
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5 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)
    
    4

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

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5 là sai. Do đó,
    if ([pass, cpass, email, cemail, user].every(x=>x!==null)) {
        // your code here ...
    }
    
    8 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)
    
    5

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

    if ([pass, cpass, email, cemail, user].every(x=>x!==null)) {
        // your code here ...
    }
    
    9 là một chuỗ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");
    }
    9 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 javascript null check - kiểm tra javascript null

    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.5742 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

    Hướng dẫn javascript null check - kiểm tra javascript null

    1

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

    if (variable === null)
    
    00 bởi vì nếu bạn sử dụng
    if (variable === null)
    
    01, 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)
    
    7

    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ị

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    8 hoặc
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    7 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");
    }
    2. 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 javascript null check - kiểm tra javascript null

    Naeem Baghinaireem BaghiNaeem Baghi

    7732 Huy hiệu vàng12 Huy hiệu bạc27 Huy hiệu đồng2 gold badges12 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)
    
    8

    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

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    8, chẳng hạn như
    if (variable === null)
    
    06. 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

    Hướng dẫn javascript null check - kiểm tra javascript null

    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
    
    0

    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à

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6. Vì vậy, chúng tôi có thể kiểm tra biến ngay cả khi nó sẽ là một ____108 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
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    6. so we can check variable even if it would be an
    if (variable === null)
    
    08 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)
    
    09 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
    
    1

    Thử-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
    
    2

    Mộ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

    Hướng dẫn javascript null check - kiểm tra javascript null

    Kaleem ullahkaleem ullahKaleem Ullah

    6.4513 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
    
    4

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5 hoạt động như một
    if (variable === null)
    
    11 và
    if (variable === null)
    
    08 cùng một lúc. So sánh kết quả
    if (variable === null)
    
    13 hoặc
    if (variable === null)
    
    14 trong
    if (variable === null)
    
    09. So sánh
    if (variable === null)
    
    16 hoặc
    if (variable === null)
    
    17 hoặc
    if (variable === null)
    
    18 sẽ dẫn đến sai. Nhưng vì
    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5 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
    
    6

    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ư

    if (variable === null)
    
    20 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
    
    7

    Tất nhiên nếu bạn thích bạn có thể sử dụng

    [pass,cpass,email,cemail,user].some(x=> x===null) 
    
    5 hoặc ________ 122 ... 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.7665 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

    3295 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

    Hướng dẫn javascript null check - kiểm tra javascript null

    CodieecodieeCodiee

    2.9012 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

    Hướng dẫn javascript null check - kiểm tra javascript null

    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