Short circuting with &&:- JavaScript




In this example also we are used two function and both are return same result, but we are recommended to use second one, compare both and think you can identify why we are recommended the second one.

Code:-


  • <!DOCTYPE html> 

    1. <html> 
    2.  
    3. <head> 
    4.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
    5.  
    6.  <script> 
    7.   /* 
    8.    Both function are same functionality, But we are recommended the second function 
    9.    */ 
    10.   function wrng_way(age) 
    11.   { 
    12.    if (age && age > 17) 
    13.    { 
    14.     alert(true); 
    15.    } 
    16.    else 
    17.    { 
    18.     alert(false); 
    19.    } 
    20.   } 
    21.  
    22.   function corct_way(age) 
    23.   { 
    24.    return alert(age && age > 17); 
    25.   } 
    26.  </script> 
    27.  
    28. </head> 
    29.  
    30. <body> 
    31.  <button onclick="corct_way(45);">Call Correct way</button> 
    32.  <button onclick="wrng_way(45);">Call Wrong Way</button> 
    33.  
    34. </body> 
    35.  
    36. </html>
    Short circuting with &&:- JavaScript Short circuting with &&:- JavaScript Reviewed by Network security on November 11, 2019 Rating: 5

    No comments:

    Useful Information

    Powered by Blogger.