JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • Using multiple ternary operators
  • File Location
  • Practice
  • EXTRA CHALLENGE
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 5-Expressions and Operators

Ternary

The ternary operator is a shorthand method of writing conditional statements (if/else). The syntax for a ternary operator is:

(x > 0) ? y : z

In this example, the conditional statement is x > 0. If the condition is true, then value y will print to the console. If it is false, value z will print instead. This is the same as:

if (x > 0) {
    return y;
} else {
    return z;
}

While it is useful for condensing code, it can be difficult to read. Be careful when using it to ensure that the code does what you want it to.

Using multiple ternary operators

You can string together several ternary operator statements in place of if/else statements, like so:

//Conditional statements
if (x == 0) {
    console.log("hello");
} else if (x < 0) {
    console.log("hi");
} else if {
    console.log("goodbye");
}

//Ternary operators

console.log((x == 0) ? "hello" : (x < 0) ? "hi" : "goodbye");

In this case, if x == 0 is true, the first option is returned, and hello is printed to the console. If it is false, it moves to the second option, which is another conditional. If x is less than 0, the first option "hi" is returned, whereas if x is greater than 0, the second option "goodbye" is returned instead. Ternary operators can be chanined together indefinetly, just as if/else statements can be repeated over and over for different conditions.

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
            └── 2-ControlFlow-and-ErrorHandling
            └── 3-Loops
            └── 4-Functions
            └── 5-Expressions-and-Operators
                01-assignment.js 
                02-comparision.js 
                03-ternary.js <----You will be working in this file.

Practice

  1. In ternary.js, re-write challenge #3 from the previous module using ternary operators.

  2. "FizzBuzz" is a classic scripting challenge for many languages.

  3. Several previous students have even been asked to solve this problem in technical interviews.

  4. Write a program using conditional statements to print the following using the numbers 1-100:

If a number is divisible by 3, print Fizz to the console. If a number is divisible by 5, print Buzz to the console. If a number is divisible by 3 AND 5, print FizzBuzz to the console. Otherwise, print the number to the console.

EXTRA CHALLENGE

Re-write the "FizzBuzz" program using ternary operators.

PreviousComparisonNextTypeof

Last updated 7 years ago