Skip to content

Instantly share code, notes, and snippets.

@ryanorsinger
Last active December 3, 2022 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanorsinger/e843c7d0966993bd8193f0afabe16ae0 to your computer and use it in GitHub Desktop.
Save ryanorsinger/e843c7d0966993bd8193f0afabe16ae0 to your computer and use it in GitHub Desktop.
Example Functions in JavaScript
// Example functions with comparison and logical operators
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#equality_operators
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#comparison_operators
// returns true or false if the input variable is greater than 2.
// 3 > 2 is true
// 2.01 > 2 is true
// 2 > 2 is false because 2 is not greater than 2
// 1 > 2 is false
function isGreaterThanTwo(x) {
return x > 2;
}
// returns true or false if the input variable is less than 2.
function isLessThanTwo(x) {
return x < 2;
}
function isTwo(x) {
return x == 2;
}
function isNotTwo(n) {
return n != 2;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT
function not(x) {
return !x;
}
function notNot(x) {
return !!x;
}
function isEmptyString(str) {
return str == "";
}
function aintEmptyString(str) {
return str != "";
}
function isTheLetterA(str) {
str = str.toLowerCase();
return str == "a";
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR
function isPorQ(x) {
x = x.toLowerCase();
return x == "p" || x == "q";
}
function isLessThanFive(x) {
return x < 5;
}
function isGreaterThanFive(x) {
return x > 5;
}
function isGreaterThanOrEqualToFive(x) {
return x >= 5;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND
function betweenTenAndTwenty(x) {
return 10 < x && x < 20;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR
function is6or12(x) {
return x === 6 || x == 12;
}
// Notice each condition, each predicate, will live on either the left or the right side of an AND or an OR operator.
// What would happen if we had 3 different conditions?
// The following code will check for a number above 1, below 10, and that the number is even.
function isAboveOneLessThan10AndEven(x) {
return x > 1 && x < 10 && x % 2 == 0;
}
// Another approach to building up multiple conditions is to build tiny functions and use them together.
// Consider the above example written with 3 helper functions
function isGreaterThan1(x) {
return x > 1;
}
function isLessThan10(x) {
return x < 10;
}
function isEven(x) {
return x % 2 == 0;
}
// Notice how this approach utilizes previously built functions in order to produce code that reads more like English.
function isAboveOneLessThan10AndEven(x) {
return isGreaterThan1(x) && isLessThan10(x) && isEven(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment