Is else required in this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is else required in this code

function checkAge(age) { if (age > 18) { return true; } else { return confirm('Did parents allow you?'); } } Will the function work differently if else is removed? function checkAge(age) { if (age > 18) { return true; } return confirm('Did parents allow you?'); }

30th Jan 2021, 7:37 PM
Dev Sharma
Dev Sharma - avatar
5 Answers
+ 3
There are only 2 scenarios. 1) age > 18 If this is true, it returns true. 2) age < 18 If case 1 fails, this is by default true. You don't have to include else because in both case it will exit as soon as it hits the return statement.
30th Jan 2021, 7:48 PM
Avinesh
Avinesh - avatar
+ 3
It is not required because u re using the return keyword which will end the execution anyway otherwise if u were just printing a message the else required to show two different messages
30th Jan 2021, 7:52 PM
Nassera
Nassera - avatar
+ 1
Thank you so much TheGirlHasNoName you cleared my doubt.
31st Jan 2021, 1:18 AM
Dev Sharma
Dev Sharma - avatar
0
I am looking for bit explained.
30th Jan 2021, 7:43 PM
Dev Sharma
Dev Sharma - avatar
0
Thanks Avinesh
31st Jan 2021, 1:17 AM
Dev Sharma
Dev Sharma - avatar