“встроенный оператор IF JavaScript” Ответ

JS Inline If

var a = 2;
var b = 3;    
var c = ((a < b) ? 'minor' : 'major');
Sticky Pingu

Как сделать одну строку, если в JS

// If statement
// if (condition) //code
if (5 > 3) return "Is greater"; // "Is greater"

// If else statement
// (condition) ? if : else
let res = (1 > 3) ? "is greater" : "is less than";// "is less than"
Unixer

встроенный оператор IF JavaScript

You can also approximate an if/else using only Logical Operators.

(a && b) || c
The above is roughly the same as saying:

a ? b : c
And of course, roughly the same as:

if ( a ) { b } else { c }
I say roughly because there is one difference with this approach, in that you have to know that the value of b will evaluate as true, otherwise you will always get c. Bascially you have to realise that the part that would appear if () { here } is now part of the condition that you place if ( here ) {  }.
Vast Vendace

Ответы похожие на “встроенный оператор IF JavaScript”

Вопросы похожие на “встроенный оператор IF JavaScript”

Больше похожих ответов на “встроенный оператор IF JavaScript” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования