GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — JavaScript Predicates - Boolean Functions Explained JavaScript predicatelast modified last modified October 18, 2023 In this article we show how to use predicates in JavaScript. A predicate in general meaning is a statement about something that is either true or false. In programming, predicates represent single argument functions that return a boolean value. JS predicate with find
The main.js
function isPositive(e) {
return e > 0;
}
let vals = [-2, -3, 0, 4, 3, -1, 1, 7];
let res = vals.find(isPositive);
console.log(res);
let res2 = vals.find(e => e > 0);
console.log(res2);
In the example, we find the first positive value in the array. function isPositive(e) {
return e > 0;
}
The first predicate is a classic function definition. let res2 = vals.find(e => e > 0); The second predicate is an anonymous arrow function. $ node main.js 4 4 JS predicate with filter
The main.js
let vals = [-2, -3, 0, 4, 3, -1, 1, 7]; let pos = vals.filter(e => e > 0); console.log(pos); let neg = vals.filter(e => e < 0); console.log(neg); let evs = vals.filter(e => e % 2 === 0); console.log(evs); In the example, we filter out positive values, negative values, and even values. $ node main.js [ 4, 3, 1, 7 ] [ -2, -3, -1 ] [ -2, 0, 4 ] JS predicate with every/some
The main.js
let vals = [-2, -3, 0, 4, 3, -1, 1, 7];
if (vals.every(e => e > 0)) {
console.log('all values are positive');
} else {
console.log('not all values are positive');
}
if (vals.some(e => e > 0)) {
console.log('at least one value is positive');
} else {
console.log('no value is positive');
}
In the example we check, if all values of the array are positive and if at least one value is positive. $ node main.js not all values are positive at least one value is positive JS negating predicates
In the next example, we define a main.js
function negate(other) {
return e => { return !other(e) };
};
let vals = [-2, -3, 0, 4, 3, -1, 1, 7];
let res = vals.filter(negate(e => e > 0));
console.log(res);
let res2 = vals.filter(negate(negate(e => e > 0)));
console.log(res2);
We use the function negate(other) {
return e => { return !other(e) };
};
In the let res = vals.filter(negate(e => e > 0)); We negate the output of the arrow function, which returns positive values. let res2 = vals.filter(negate(negate(e => e > 0)));
We apply the $ node main.js [ -2, -3, 0, -1 ] [ 4, 3, 1, 7 ] SourceJavaScript - language reference In this article we have worked with predicates in JavaScript. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |