jquery - JavaScript ternary operator into full if/else statement issue -
jquery - JavaScript ternary operator into full if/else statement issue -
i have next ternary statement:
$.history.init(function(url) { load(url == "" ? "#some-page" : url); }); which have rewrote into:
$.history.init(function(url) { load( if( url == ""){ url = "#some-page" } else { url = url } ); }); i error on line 3 if(url == ""), don't understand error. suggestion much appreciated.
in javascript, if not expression. not homecoming value , cannot set within function call. is, not valid:
func(if (a) { ... } else { ... }); this main difference between if , ?:--the operator look , returns value; if statement, not homecoming value , cannot used everywhere.
your best bet if have avoid ternary operator like:
if (url == "") { url = "#some-page"; } load(url); you can accomplish same effect using ||:
function (url) { load(url || "#some-page"); } this shortest , idiomatic way write code.
javascript jquery if-statement ternary-operator
Comments
Post a Comment