How to negate a predicate function in JavaScript ? Last Updated : 30 Dec, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to negate a predicate function in JavaScript. Predicate functions are the ones that check the condition and return true and false based on the argument. Our task is to get the opposite of the predicate function. We follow the following method to get the desired result. Method 1: Our Predicate function is checking for odd and even numbers. If the number is a module with 2, it returns 1 then it is odd, else it is even. Negate the logic while checking conditions for argument. Example: This example shows the above-explained approach. JavaScript <script> // Predicate function check odd function isOdd(number) { return number % 2 == 1; } // negation of isOdd function function isNotOdd(number) { return number % 2 !== 1; } // Predicate function check Even function isEven(number) { return number % 2 == 0; } // negation of isEven function function isNotEven(number) { return number % 2 !== 0; } // Outputs: true console.log(isOdd(5)); // Outputs: true console.log(isNotOdd(2)); // Output: false console.log(isEven(3)); // Output> false console.log(isNotEven(4)); </script> Output: true true false false Method 2: The problem with the above method is to we are hard-coding our logic at each negation. We probably have more chances to make mistakes in negation conditions. More effective is to negate the predicate function by checking the condition. Example: This example shows the above-explained approach. JavaScript <script> // Predicate function check odd function isOdd(number) { return number % 2 == 1; } // negation of isOdd function function isNotOdd(number) { return !isOdd(number); } // Predicate function check Even function isEven(number) { return number % 2 == 0; } // negation of isEven function function isNotEven(number) { return !isEven(number); } // Outputs: true console.log(isOdd(5)); // Outputs: true console.log(isNotOdd(10)); // Output: false console.log(isEven(3)); // Output> false console.log(isNotEven(4)); </script> Output : true true false false Method 3: In the previous method, we are negating the function for all the predicate functions. But our solution can be more effective if we create one function that negates all the predicate functions. We make a predicate function and bind negate function with all predicate functions. Example: This example shows the above-explained approach. JavaScript <script> // Predicate function check odd function isOdd(number) { return number % 2 == 1; } // Predicate function check Even function isEven(number) { return number % 2 == 0; } // function that negate all function function negate(pre) { return function (number) { return !pre(number); }; } // Wrapping predicate function to negate function var isNotOdd = negate(isOdd); var isNotEven = negate(isEven); // Outputs: true console.log(isOdd(5)); // Outputs: true console.log(isNotOdd(10)); // Output: false console.log(isEven(3)); // Output: false console.log(isNotEven(4)); </script> Output : true true false false Comment More infoAdvertise with us Next Article How to negate a predicate function in JavaScript ? S satyam00so Follow Improve Article Tags : JavaScript Web Technologies javascript-functions JavaScript-Questions Similar Reads What is Currying Function in JavaScript? Currying is used in JavaScript to break down complex function calls into smaller, more manageable steps. It transforms a function with multiple arguments into a series of functions, each taking a single argument.It converts a function with multiple parameters into a sequence of functions.Each functi 3 min read How to call JavaScript function in HTML ? In HTML, you can easily call JavaScript functions using event attributes like onclick and onload. Just reference the function name within these attributes to trigger it. You can also call functions directly within script blocks using standard JavaScript syntax. Let's create an HTML structure with so 2 min read setTimeout() in JavaScript The setTimeout() function is used to add delay or scheduling the execution of a specific function after a certain period. It's a key feature of both browser environments and Node.js, enabling asynchronous behavior in code execution.JavaScriptsetTimeout(function() { console.log('Hello, world!'); }, 2 2 min read What is a typical use case for anonymous functions in JavaScript ? In this article, we will try to understand what exactly an Anonymous function is, and how we could declare it using the syntax provided in JavaScript further we will see some examples (use-cases) where we can use anonymous functions to get our results in the console. Before proceeding with the examp 4 min read How to check whether a number is NaN or finite in JavaScript ? When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN 3 min read How to Encode and Decode a URL in JavaScript? Encoding and decoding URLs in JavaScript is essential for web development, especially when making GET requests with query parameters. This process ensures special characters in URLs are correctly interpreted by the server. For instance, spaces are converted to %20 or + in URLs. This guide covers how 4 min read How to declare the optional function parameters in JavaScript ? Declaring optional function parameters in JavaScript means defining function parameters that aren't required when the function is called. You can assign default values to these parameters using the = syntax, so if no argument is provided, the default value is used instead. These are the following ap 3 min read How to get the javascript function parameter names/values dynamically ? In this article, we are given any arbitrary JavaScript function and the task is to return the parameter names of the function. Approach: JavaScript contains a method called toString() which is used to represent a function code in its string representation. This method is used to get the parameter na 2 min read How To Include a JavaScript File in Another JavaScript File? The import and export syntax available in ES6 (ECMAScript 2015) module is used to include a JavaScript file in another JavaScript file. This approach is the most modern and recommended way to share code between JavaScript files.It allows you to break your code into smaller modules and then import th 4 min read How to override a JavaScript function ? In this article, we are given an HTML document and the task is to override the function, either a predefined function or a user-defined function using JavaScript. Approach: When we run the script then Fun() function is called. After clicking the button the GFG_Fun() function is called and this funct 2 min read Like