JavaScript ternary if statement

I would like to know what is shortcode if in javascript?

Like in php:

$res = ($x > $y)? $x: $y;

What is its conversion to javascript?

+3
source share
5 answers

This is the same in javascript :)

var res = (x > y) ? x : y;
+16
source
var x = 2;
var y = 3;
var res = (x > y)? x: y;

Although perhaps the following would be better:

var res = Math.max(x, y);
+4
source

Same. It is called triple:

var x = 10, y = 50, res = 0; 
res = (x > y) ? x : y; 
alert(res);
+3
source

This is the same in javascript:

res = (y < x) ? x : y; or res = (x > y) ? x : y;

+2
source

Here you: var res = x>y?x:y;

0
source

All Articles