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:-
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script>
- /*
- Both function are same functionality, But we are recommended the second function
- */
- function wrng_way(age)
- {
- if (age && age > 17)
- {
- alert(true);
- }
- else
- {
- alert(false);
- }
- }
- function corct_way(age)
- {
- return alert(age && age > 17);
- }
- </script>
- </head>
- <body>
- <button onclick="corct_way(45);">Call Correct way</button>
- <button onclick="wrng_way(45);">Call Wrong Way</button>
- </body>
- </html>
Short circuting with &&:- JavaScript
Reviewed by Network security
on
November 11, 2019
Rating:
No comments: