Ternary operator javascript

What is wrong with this code? if I call this function without any arguments, the expected output is "critical" instead "medium" is returned.

let labelBlock = (label="") =>{
        label = label.toLowerCase();
        const txt = label == "high" ? "High" : "medium" ? "Medium" : "Critical";
        return `<span class="${txt.toLowerCase()}">${txt}</span>`;
      }

label is empty at the start. The first condition label == "high" is false and in the second condition you have "medium", which, in Js, is truthy value hence "Medium" is set.

You can change that line to the following to get Critical. See change (label == "medium")

const txt = label == "high" ? "High" : label == "medium" ? "Medium" : "Critical";

Make Your Code Cleaner with JavaScript Ternary Operator, The Conditional (Ternary) Operator � The condition is what you're actually testing. The result of your condition should be true or false or at least� JavaScript Ternary Operator In this tutorial, you will learn about the conditional/ternary operator in JavaScript with the help of examples. A ternary operator can be used to replace if..else statement in certain situations.

You need to replace "medium" with "label == 'medium'".

let labelBlock = (label="") =>{
    label = label.toLowerCase();
    const txt = label == "high" ? "High" : label == "medium" ? "Medium" : "Critical";
    return `<span class="${txt.toLowerCase()}">${txt}</span>`;
}

"medium" expression in the second part is always "true" in your code. So the second ternary operator always returns the first value ("Medium").

JavaScript — The Conditional (Ternary) Operator Explained, The conditional ternary operator in JavaScript assigns a value to a variable based on some condition and is the only JavaScript operator that� JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more. Conditional (Ternary) Operator. The conditional operator

There is no condition which is evaluated at the second part "medium" ? "Medium" : "Critical";

Hope this works for you

        const txt = label == "high" ? "High" : label == "medium" ? "Medium" : "Critical";

The JavaScript Ternary Operator as a Shortcut for If/Else Statements, Conditional (Ternary) Operator. JavaScript also contains a conditional operator that assigns a value to a variable based on some condition. The JavaScript ternary operator is the only operator that takes three operands. The condition is an expression that evaluates to a Boolean value, either true or false. If the condition is true, the ternary operator returns expression_1, otherwise it returns the expression_2. The expression_1, and expression_2 are expressions of any type.

Your ternary operation meaning is below. As you can see the problem is you are checking if ("medium").

  var txt;
  if (label == "high") {
    txt = "High";
  } else {
    if ("medium") {
      text = "Medium";
    } else {
      text = "Critical";
    }
  }

JavaScript Comparison and Logical Operators, The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement. Actually ternary is the type of operator (i.e. it has 3 parts). The name of that specific ternary operator is the conditional operator. There just happens to only be one ternary operator in JS so the terms get misused. – Gone Coding Nov 10 '14 at 10:10

How do you use the ? : (conditional) operator in JavaScript?, JavaScript has several different types of operators. These include comparison, arithmetic, logical, and many more. These operators can be� The ternary operator "?:" earns its name because it's the only operator to take three operands. It is a conditional operator that provides a shorter syntax for the if..then..else statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third

The JavaScript Ternary Operator. What it does and why you should , Edpresso Editor. August 27, 2020. The conditional ternary operator assigns a value to a variable based on some condition. It is used in place of the if statement . Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

What is the ternary operator in JavaScript?, The ternary operator is the only operator in JavaScript that works with 3 operands , and it's a short way to express conditionals. This is how it� Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner replacement for if-then-else statement and used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators.