How to test a value x against predicate function and returns fn(x) or x in JavaScript?

Testing a value x against a predicate function means checking if x meets a specific condition. If the condition is true, we apply a transformation function to x and return the result. Otherwise, we return the original value x unchanged.

Syntax

The basic syntax uses the ternary operator to test a value against a predicate function:

let result = predicate(x) ? operation(x) : x;

Where predicate(x) returns true/false, and operation(x) transforms the value when the predicate passes.

Basic Example

Here's a simple example that squares numbers greater than 5, otherwise returns the original value:

<html>
<body>
    <h3>Testing a value x against predicate function</h3>
    <div id="content"></div>
    <script>
        let content = document.getElementById("content");
        
        function operation(x) {
            return x * x; 
        }
        
        function predicate(x) {
            return x > 5;
        }
        
        let x = 10;
        let result = predicate(x) ? operation(x) : x;
        content.innerHTML = "Original value: " + x + ", Result: " + result + "<br>";
        
        let x2 = 3;
        let result2 = predicate(x2) ? operation(x2) : x2;
        content.innerHTML += "Original value: " + x2 + ", Result: " + result2 + "<br>";
    </script>
</body>
</html>

Advanced Example with Function Parameters

We can create a reusable test function that accepts predicate and operation functions as parameters:

<html>
<body> 
    <h3>Flexible predicate testing with function parameters</h3>
    <div id="output"></div>
    <script>
        let output = document.getElementById("output");
        
        // Operation functions
        function halve(x) {
            return x / 2;
        }
        
        function makePositive(x) {
            return Math.abs(x);
        }
        
        // Predicate functions
        function isEven(x) {
            return x % 2 === 0;
        }
        
        function isNegative(x) {
            return x < 0;
        }
        
        // Generic test function
        function test(x, predicate, operation) {
            output.innerHTML += "Testing value: " + x + "<br>";
            let result = predicate(x) ? operation(x) : x;
            output.innerHTML += "Result: " + result + "<br><br>";
            return result;
        }
        
        // Test cases
        test(6, isEven, halve);      // 6 is even, so halve it ? 3
        test(5, isEven, halve);      // 5 is odd, return original ? 5  
        test(-8, isNegative, makePositive); // -8 is negative, make positive ? 8
        test(10, isNegative, makePositive); // 10 is positive, return original ? 10
    </script>
</body> 
</html>

Common Use Cases

Predicate Operation Use Case
x > threshold clamp(x) Limit maximum values
x < 0 Math.abs(x) Convert negative to positive
isString(x) x.toUpperCase() Transform strings only

Conclusion

Using predicates with the ternary operator provides a clean way to conditionally transform values in JavaScript. This pattern is useful for validation, data transformation, and implementing conditional logic in a functional programming style.

Updated on: 2026-03-15T23:19:01+05:30

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements