javascript - Why we cannot have return in ternary operator? -
say i've simple form , want check whether form has changed or not. if changed submit else prevent form submission, used return , instead of using if-else statement tried use ternary operation unfortunately hit error uncaught syntaxerror: unexpected token return did not understand why error? ternary operation used assign? not sure on part. below sample of trying do.
var form_original_data = $("#frmprofile").serialize(); $("#frmprofile").on('submit', function(e) { e.preventdefault(); $("#frmprofile").serialize() != form_original_data ? $("body").append('changed') : return; }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="frmprofile"> <input type="text" value="name" /> <input type="submit" value="go" /> </form>
the ternary operator evaluates expression , expressions can't contain return statement (how behave if assign expression variable?). however, return result of ternary operator, i.e. return condition? returnvalue1 : returnvalue2;
on specific point, don't see why return. looks you're trying if condition fulfilled. simple if statement more adequate there.
Comments
Post a Comment