Skip to content
Home ยป JavaScript Exercise 23: Conditional (ternary) operator “?”

JavaScript Exercise 23: Conditional (ternary) operator “?”

function test() {
    const age = 14;
    const isAdult=age>=18?true:false;
    if (isAdult) {
        console.log("You are an adult!")
    } else {
        console.log("You are not an adult yet.")
    }
}

When developing software applications, we frequently need to make decisions based on specific conditions. JavaScript provides several mechanisms for implementing conditional logic, including the if...else statement and the ternary operator.

In this exercise, we combine both mechanisms to determine whether a person is an adult based on their age.

The JavaScript Code

function test() {
    const age = 14;
    const isAdult = age >= 18 ? true : false;

    if (isAdult) {
        console.log("You are an adult!");
    } else {
        console.log("You are not an adult yet.");
    }
}

1. Declaring the Age

const age = 14;

We start by declaring a constant named age and assigning it the numerical value 14.

The const keyword is used because the value is not reassigned during the execution of the function.

2. Understanding the Ternary Operator

const isAdult = age >= 18 ? true : false;

The ternary operator, also known as the conditional operator, evaluates a condition and returns one of two possible values.

Its general syntax is:

condition ? valueIfTrue : valueIfFalse;

In our example, the condition checks whether the age is greater than or equal to 18.

  • If the condition is true, isAdult receives the Boolean value true.
  • If the condition is false, isAdult receives the Boolean value false.

Since the age is 14, the constant isAdult receives the value false.

The ternary operator is called ternary because it takes three operands: a condition, a value for a true condition and a value for a false condition.

3. Evaluating the Boolean Value

if (isAdult) {
    console.log("You are an adult!");
} else {
    console.log("You are not an adult yet.");
}

The if...else statement evaluates the Boolean value stored in isAdult.

Depending on this value, the application displays the corresponding message in the browser console.

4. Expected Output

When executing the test() function with an age of 14, the following message appears in the console:

You are not an adult yet.

5. Simplifying the JavaScript Code

Although the ternary operator works correctly in this example, it is technically redundant.

The comparison operator >= already returns a Boolean value. Therefore, the following two expressions are equivalent:

const isAdult = age >= 18 ? true : false;
const isAdult = age >= 18;

The second expression is more concise and would generally be preferred in professional software development.

However, the first expression remains useful for learning and understanding how the ternary operator works.

6. Common Programming Mistakes

One possible mistake is evaluating a ternary expression without storing or using its result.

age >= 18 ? true : false;

Although JavaScript evaluates the expression, its result is discarded.

Another common mistake is evaluating the age directly inside the if statement.

if (age) {
    console.log("You are an adult!");
}

JavaScript considers nonzero numerical values to be truthy. Consequently, an age of 14 would incorrectly result in the message indicating that the person is an adult.

It is also important to declare constants and variables explicitly using const or let. Using an undeclared identifier can introduce unexpected behaviour or cause errors in strict mode.

7. Software Testing and Boundary Value Analysis

From a software testing perspective, this exercise provides an excellent opportunity to apply Boundary Value Analysis, a testing technique covered by the ISTQB Foundation syllabus.

The critical boundary in this application is the age of 18.

Test caseAgeExpected BooleanExpected result
TC0117falseNot an adult
TC0218trueAdult
TC0319trueAdult

Testing these three values helps verify that the comparison operator correctly handles the transition between the two categories.

For example, accidentally using > instead of >= would cause the application to incorrectly classify a person aged exactly 18.

In a production application, additional validation would also be necessary to handle invalid input, such as negative ages or nonnumerical values.

Conclusion

This JavaScript exercise demonstrates several fundamental programming concepts:

  • Declaring constants
  • Using comparison operators
  • Working with Boolean values
  • Understanding the ternary operator
  • Implementing conditional logic
  • Applying Boundary Value Analysis

Although the application is relatively simple, these concepts form the foundation of more complex software applications. Combining programming exercises with software testing techniques also helps develop a better understanding of software quality, reliability and correctness.

Leave a Reply

Your email address will not be published. Required fields are marked *