Ternary operator in JavaScript

In this lesson, we will consider the special ternary operator, which is a shorthand version of the if-else construct. Its syntax is as follows:

javascriptlet variable = condition ? value1 : value2;

The operator works like this: if the condition is true, then value1 is returned, otherwise - value2. Let's make an example code using the given operator:

javascriptlet age = 17;
let adult = age >= 18 ? true : false;
console.log(adult);

This code entirely can be rewritten as follows:

javascriptlet age = 17;
let adult;

if (age >= 18) {
  adult = true;
} else {
  adult = false;
}

console.log(adult);

The ternary operator should only be used in the most basic cases, as its use makes the code harder to understand.

Задание 1

Rewrite the following code with a ternary operator:

javascriptlet num = 1;
let res;

if (num >= 0) {
  res = '1';
} else {
  res = '2';
}

console.log(res);