Menu

gitpiper

yesNo javascript Code Snippet in 2024

stringregexpintermediate

Last Updated: 1 October 2024

Returns true if the string is y/yes or false if the string is n/no.

  • Use RegExp.prototype.test() to check if the string evaluates to y/yes or n/no.
  • Omit the second argument, def to set the default answer as no.
const yesNo = (val, def = false) => /^(y|yes)$/i.test(val) ? true : /^(n|no)$/i.test(val) ? false : def;
yesNo('Y'); // true yesNo('yes'); // true yesNo('No'); // false yesNo('Foo', true); // true

javascript snippet similar to yesNo For You in October 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️