Are curly braces necessary in one-line statements in JavaScript? -
i once heard leaving curly braces in one-line statements harmful in javascript. don't remember reasoning anymore , google search did not much. there makes idea surround statements within curly braces in javascript?
i asking, because seems so.
no
but recommended. if ever expand statement need them.
this valid
if (cond) alert("condition met!") else alert("condition not met!")
however highly recommended use braces because if (or else) ever expands statement required.
this same practice follows in c syntax style languages bracing. c, c++, java, php support 1 line statement without braces. have realize saving two characters , people's bracing styles aren't saving line. prefer full brace style (like follows) tends bit longer. tradeoff met fact have extremely clear code readability.
if (cond) { alert("condition met!") } else { alert("condition not met!") }
Comments
Post a Comment