Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
ES6 Ternary Operator | Getting Started: ES6
Introduction to React
course content

Conteúdo do Curso

Introduction to React

Introduction to React

1. Getting Started: ES6
2. Getting Started: JSX
3. React in Practice
4. React Components
5. Controlling Data & Content
6. Hooks
7. React Router

ES6 Ternary Operator

The ternary operator is a concise and readable way to express conditional statements in JavaScript. It has the following syntax:

Here the statement of interest is number % 2 == 0 ? "Even" : "Odd".

If you look closely, you will notice that it follows the syntax of the ternary operator. Here number % 2 == 0 is the expression to be evaluated. The operator returns "Even" if the expression is true, and "Odd" if the expression is false. In turn, the returned value is stored in the variable numType.

FizzBuzz problem using ternary operator (not if-else):

Task

Write a conditional using the ternary operator that returns the square of i if i is less than 5 otherwise the cube of i.

question-icon

Fill in the gaps.

for(let i = 0; i < 10; i++) {
    console.log (
        // Change code below this line
        

        // Change code above this line
    );
}
0 1 4 9 16 125 216 343 512 729

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Seção 1. Capítulo 8
We're sorry to hear that something went wrong. What happened?
some-alt