JavaScript三元if语句

Koo*_*bin 5 javascript if-statement shortcut conditional-operator

我想知道在javascript中if的短代码是什么?

喜欢在php中:

$res = ($x > $y)? $x: $y;
Run Code Online (Sandbox Code Playgroud)

它在javascript中的转换是什么?

Dar*_*lus 20

它在javascript中是一样的:)

var res = (x > y) ? x : y;
Run Code Online (Sandbox Code Playgroud)


CL2*_*L22 5

var x = 2;
var y = 3;
var res = (x > y)? x: y;
Run Code Online (Sandbox Code Playgroud)

尽管以下内容可能会更好:

var res = Math.max(x, y);
Run Code Online (Sandbox Code Playgroud)