最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Confused by shorthand syntax: x > 0 ? 1 : -1; - Stack Overflow

programmeradmin4浏览0评论

What does the following Javascript syntax mean? Please describe the whole syntax:

var x = 0;
x > 0 ? 1 : -1;  // confused about this line
alert(x);

What does the following Javascript syntax mean? Please describe the whole syntax:

var x = 0;
x > 0 ? 1 : -1;  // confused about this line
alert(x);
Share Improve this question edited Aug 14, 2012 at 0:51 Mechanical snail 30.6k14 gold badges90 silver badges113 bronze badges asked May 12, 2012 at 20:04 osamiosami 611 gold badge1 silver badge2 bronze badges 4
  • That's the [ternary operator][1] (the link explains it) [1]: stackoverflow.com/questions/1788917/javascript-ternary-operator – Dhaivat Pandya Commented May 12, 2012 at 20:05
  • 7 It does nothing... – gdoron Commented May 12, 2012 at 20:07
  • 1 @DhaivatPandya The official name is the conditional operator. A ternary operator is just one with three operands. – David Heffernan Commented May 12, 2012 at 20:23
  • Not a duplicate; the other question is about this operator's interaction with the compound assignment syntax. – Mechanical snail Commented Aug 14, 2012 at 0:54
Add a comment  | 

4 Answers 4

Reset to default 20

That on its own means nothing. You will alert x's value, which is 0, and that's it. The second statement is meaningless unless you assign it to something. If however you would have done this:

var x=0;
var y = x > 0 ? 1 : -1;
alert(y);

You would have gotten -1.

The Conditional Operator, is a shorthand for IF statements, it basically says:

Assert if x > 0. If so, assign 1. If not, assign -1.

Or on a more general form:

CONDITION ? VALUE_IF_TRUE : VALUE_IF_FALSE;

Where:

  • CONDITION - can be anything which evaluates to a boolean (even after type juggling).
  • VALUE_IF_TRUE - value to be returned in case CONDITION was asserted to TRUE.
  • VALUE_IF_FALSE - value to be returned in case CONDITION was asserted to FALSE.

That is the conditional operator. It is a ternary operator because it has three operands. It is often referred to as the ternary operator but that terminology is rather loose since any operator with three operands is a ternary operator. It just so happens that is is the only commonly used ternary operator.

What does it mean? The expression

a?b:c

evaluates to b if a evaluates as true, otherwise the expression evaluates to c.

this is a ternary operator (the ?)

Think of it like an IF statement.

the statement before the '?' is the condition of your if statement. Immediately what follows before the ':' is what will execute/be-assigned if the statement is true. After the ':' is what will execute/be-assigned if the statement is false.

Your code however will just alert 0 because you aren't assigning anything from your ternary operator.

basically your code might as well say.
x = 0; alert(x); // this would alert 0

you need to revise this to:
x = 0; var y = x > 0 ? 1 : -1; alert(y);

It will be -1. This is known as the ternary operator.

Basically it expands to this (assuming you meant to put x= at the beginning of the second line).

if(x>0){
  x = 1
} else {
  x = -1
}
发布评论

评论列表(0)

  1. 暂无评论