0

I came across this page and reading the example codes. I don't understand how the following line works in the the codes. How did it assign the value "\nlogin:" to the "error" variable when the form value is empty?

The line of code:

error+=f.login.value==''?'\nlogin':'';

The whole example code is under "2. Javascript is an enhancement, not a secure functionality".

Thanks for the help.

Pete C.
  • 135
  • 2
  • 9

1 Answers1

2

It means:

if(f.login.value == '')
    error += '\nlogin'; 
else
    error += '';

The ?: syntax is called the ternary (or conditional) operator, and works like an inline if/else statement.

bfavaretto
  • 69,385
  • 15
  • 102
  • 145
  • Thanks for answering! I saw the ? in the codes but I could not comprehend that it was the ternary operator, because it was so compact! (And the single quotes confused me too) Now I see, thanks again! – Pete C. Jun 15 '13 at 18:03