Remembering the Ternary Operator Syntax

2019-02-13 15:30发布

Anyone have a good trick to remember the standard ternary syntax?

Specifically whether the '?' or ':' comes first. I have consistently gotten this backwards over the years.

7条回答
家丑人穷心不美
2楼-- · 2019-02-13 16:36

"?" is a question mark so it means "if".

A colon means, "now it comes", "then do".

The good thing about the ternary operator is that you are not forced to use it, especially if you are having problems remembering the syntax. Just use an if-statement which is more readable most times.

And no - the ternary has no better performace then an if-statement.

查看更多
登录 后发表回答