Cara menggunakan apa itu ternary javascript?

Sayangnya JavaScript tidak menyediakan cara yang sangat singkat dan mudah dibaca untuk melakukan ini. Secara pribadi saya hanya akan menggunakan beberapa pernyataan if baris tunggal seperti ini:

var inputOneAns;
if [inputOne === 'Yes'] inputOneAns = '517';
if [inputOne === 'No'] inputOneAns = '518';
else inputOneAns = '';

Yang bahkan bisa lebih bersih jika Anda abstrak menjadi fungsi [catatan: tidak perlu else dalam kasus ini]:

function getInputOneAns[inputOne] {
    if [inputOne === 'Yes'] return '517';
    if [inputOne === 'No'] return '518';
    return '';
}

Secara pribadi, saya tidak begitu suka pernyataan pergantian karena ini karena dua alasan: pertama pernyataan ekstra break yang membengkak kode, dan kedua, pernyataan pergantian sangat membatasi - Anda hanya dapat melakukan pemeriksaan kesetaraan sederhana, dan hanya terhadap satu variabel. Selain itu, jika Anda tahu Anda akan selalu memeriksa satu string, saya akan menyukai objek peta sederhana:

var map = { 'Yes': '517', 'No': '518' };
var inputOneAns = map[inputOne] || '';

The conditional [ternary] operator is the only JavaScript operator that takes three operands: a condition followed by a question mark [?], then an expression to execute if the condition is truthy followed by a colon [:], and finally the expression to execute if the condition is falsy. This operator is frequently used as an alternative to an if...else statement.

Try it

Syntax

condition ? exprIfTrue : exprIfFalse

Parameters

condition

An expression whose value is used as a condition.

exprIfTrue

An expression which is executed if the condition evaluates to a truthy value [one which equals or can be converted to true].

exprIfFalse

An expression which is executed if the condition is falsy [that is, has a value which can be converted to false].

Description

Besides false, possible falsy expressions are: null, NaN, 0, the empty string [""], and undefined. If condition is any of these, the result of the conditional expression will be the result of executing the expression exprIfFalse.

Examples

A simple example

const age = 26;
const beverage = age >= 21 ? "Beer" : "Juice";
console.log[beverage]; // "Beer"

Handling null values

One common usage is to handle a value that may be null:

const greeting = [person] => {
  const name = person ? person.name : "stranger";
  return `Howdy, ${name}`;
}

console.log[greeting[{ name: "Alice" }]];  // "Howdy, Alice"
console.log[greeting[null]];             // "Howdy, stranger"

Conditional chains

The ternary operator is right-associative, which means it can be "chained" in the following way, similar to an if … else if … else if … else chain:

function example[] {
  return condition1 ? value1
        : condition2 ? value2
        : condition3 ? value3
        : value4;
}

This is equivalent to the following if...else chain.

function example[] {
  if [condition1] {
    return value1;
  } else if [condition2] {
    return value2;
  } else if [condition3] {
    return value3;
  } else {
    return value4;
  }
}

Specifications

Specification
ECMAScript Language Specification
# sec-conditional-operator

Browser compatibility

BCD tables only load in the browser

See also

Apa itu ternary di JavaScript?

6. Opeartor Ternary pada Javascript Operator ternary merupakan operator yang teridiri dari tiga bagian. Operator-operator sebelumnya hanya dua bagian saja, yaitu: bagian kiri dan kanan. Ini disebut operator binary. Sementara operator trinary ada bagian kiri, tengah, dan kanan.

Apa itu operator dalam JavaScript?

Operator adalah instruksti yang digunakan untuk melakukaknsuatu proses, misal pada operator assignment[=] yang digunakan untuk memasukkan nilai kedalam variabel, operator koma[,] yang digunakan pada deklarasi variabel[ var a,b,c; ].

Apa yang anda ketahui tentang operator ternary jelaskan beserta bentuk umumnya?

Operator ternary adalah operator yang digunakan dalam operasi yang melibatkan tiga buah operand. Adapun operator yang digunakan untuk menyatakannya adalah operator "?:". Konsep yang mendasari operasi ini adalah sebuah percabangan [pemilihan] yang didasarkan atas kondisi tertentu.

Bài mới nhất

Chủ Đề