DEV Community 👩‍💻👨‍💻

Cover image for Day 85/100 Ternary Operator
Rio Cantre
Rio Cantre

Posted on • Updated on • Originally published at

Day 85/100 Ternary Operator

There are endless possibilities if one would try to do great to achieve a particular goal and put tons of trial and error to the test.


We can use a ternary operator to perform this same functionality:

isNightTime ? console.log('Turn on the lights!') : console.log('Turn off the lights!');
Enter fullscreen mode Exit fullscreen mode

In the example above:

  • The condition, isNightTime, is provided before the ?.
  • Two expressions follow the ? and are separated by a colon :.
  • If the condition evaluates to true, the first expression executes.
  • If the condition evaluates to false, the second expression executes.

Code Snippets

let userName = 'Teddy';

if(userName) {
  console.log('Hello There!');

let userQuestion = 'Why is the sky blue?';
console.log(`${userName}, I have a question for you. ${userQuestion}`);

let randomNumber = Math.floor(Math.random() * 8);
// console.log(randomNumber);

let eightBall = '';
eightBall = randomNumber;

switch (eightBall) {
  case 8:
    console.log('It is certain');
  case 7:
    console.log('It is decidedly so');
  case 6:
    console.log('Reply hazy try again');
  case 5:
    console.log('Cannot predict now');
  case 4:
    console.log('Do not count on it');
  case 3:
    console.log('My sources say no');
  case 2:
    console.log('Outlook not so good');
  case 1:
    console.log('Signs point to yes');

Enter fullscreen mode Exit fullscreen mode

a11y myths

Accessibility is only about adding alternative text to images

In fact, missing alternative text for images is one of the biggest accessibility issues. But besides this, there are many things to be considered while making accessible website - headline structure, functional controls, color contrast and much more.


Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.