Why is correct the way it is written in the course? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is correct the way it is written in the course?

In the Form Validation example of the JS course it uses the following code: function validate() { var n1 = document.getElementById("num1"); var n2 = document.getElementById("num2"); if(n1.value != "" && n2.value != "") { if(n1.value == n2.value) { return true; } } alert("The values should be equal and not blank"); return false; } For my understanding, programming is kind of linear (line two comes after line one unless you call it first). So, in that function there are two IF statements. In case both statements are TRUE the RETURN value is set to TRUE as well. So far so good. Yet, why is the second part inside the function not being executed after the IF conditions are TRUE? Shouldn't it be executed anyway since there is no ELSE statement telling the program to not execute the remaining code after the IF statement is TRUE? I am sorry if this questions seems kind of basic to you guys.

8th Feb 2018, 1:27 PM
Diego Fdez
Diego Fdez - avatar
1 Answer
0
That's because if both 'if' statements are true, a return statement will be encountered which means the code execution inside the function will end there and return the value which is 'true'. It will not execute the next lines of code.
8th Feb 2018, 1:37 PM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar