“operador ternario de JavaScript” Código de respuesta

JavaScript ternary

condition ? doThisIfTrue : doThisIfFalse

1 > 2 ? console.log(true) : console.log(false)
// returns false
Adventurous Ant

operador ternario de JavaScript

let showme || "if the variable showme has nothing inside show this string";
let string = condition ? 'true' : 'false'; // if condition is more than one enclose in brackets
let condition && 'show this string if condition is true';
KingUche

Operador ternario en JavaScript

let amount = 50;
let food = amount > 100 ? 'buy coka-cola' : 'buy just a water bottle';
console.log(food)
Ariful Islam Adil(Code Lover)

JavaScript JS Ternary Operer

<script>  
    function gfg() {  
     //JavaScript to illustrate 
    //Conditional operator 
  
    let PMarks = 40 
    let result = (PMarks > 39)? 
        "Pass":"Fail"; //Syntax:- (condition) ? do this if true : do this if false
  
    document.write(result); 
    }  
    gfg();  
</script>
Dangerous Dunlin

operador ternario JavaScript

// ternary operator in javascript
const x = 6;
let answer = x > 10 ? "greater than 10" : "less than 10";
console.log(answer);
// output: less than 10

// nested condition
const answer = x > 10 ? "greater than 10" : x < 5 ? "less than 5" : "between 5 and 10";
console.log(answer);
// output: between 5 and 10

// Syntax
condition ? ifTrue : ifFalse
Chetan Nada

Expresiones ternarias en JavaScript

let memberType = 'basic';
let price = memberType === 'basic' ? 5 : 10;

In the example, the condition that is evaluated is whether memberType === 'basic'. 
If this condition is true, then price will be 5, and otherwise it will be 10. 
The equivalent long-hand conditional expression would be:

let memberType = 'basic';
let price;

if (memberType === 'basic') {
  price = 5;
} else {
  price = 10;
}
Gorgeous Goldfinch

Respuestas similares a “operador ternario de JavaScript”

Preguntas similares a “operador ternario de JavaScript”

Más respuestas relacionadas con “operador ternario de JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código