Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære ES6 Ternary Operator | Getting Started: ES6
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to React

bookES6 Ternary Operator

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

expression ? value_if_true : value_if_false
123
let number = 10; let numType = number % 2 == 0 ? "Even" : "Odd"; console.log (numType);
copy

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):

123456789
for(let i = 1; i < 17; i++) { console.log( i % 3 == 0 && i % 5 == 0 ? "FizzBuzz" : ( i % 3 == 0 ? "Fizz" : ( i % 5 == 0 ? "Buzz" : i ) ) ); }
copy

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

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 8

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show me how to write the ternary operator for this task?

What would the code look like for a specific value of i, like i = 3?

Can you explain how the ternary operator chooses between square and cube in this case?

bookES6 Ternary Operator

Sveip for å vise menyen

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

expression ? value_if_true : value_if_false
123
let number = 10; let numType = number % 2 == 0 ? "Even" : "Odd"; console.log (numType);
copy

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):

123456789
for(let i = 1; i < 17; i++) { console.log( i % 3 == 0 && i % 5 == 0 ? "FizzBuzz" : ( i % 3 == 0 ? "Fizz" : ( i % 5 == 0 ? "Buzz" : i ) ) ); }
copy

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

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 8
some-alt