Simple JavaScript precondition checking implementation
function requires(conditions)
{
    for (var cond in arguments)


Don't use for-in. The problem with for-in is that it runs over prototype properties as well. You might loop through more properties than you expect. Either use a regular loop or convert your iterable into an actual array to access array method.

If you can write in ES6, you can use the rest operator. What it does is put the rest of the arguments into an array. Much like arguments but it's a real array. You have access to all array methods.

function requires(...args){
  args.forEach(...)




if (typeof cond !== "boolean") throw "Preconditions must be boolean expressions {" + cond + "}";
if (cond !== true) throw "Precondition failed {" + cond + "}";


The second condition is overly verbose. The first check weeds conds that are not boolean. Therefore, if it passes that part, cond is a boolean. That means you can simplify if (cond !== true) as if (!cond).



requires(
    typeof message === "string",
    typeof index === "number",
    index &gt;= 0,
    index &lt; message.length);


If you go on with this route of manual type checks, at least hide away the complexity by abstracting the checks away. The following would be more like it. Checks the arguments against the type, and in that order.

require(args, ['string', 'number']);


Additionally, the last 2 checks there are not type checks but more like app-specific logic. Your type checker should not concern itself with those anymore.



Runtime type checks should be avoided as they incur performance penalties as well as cause bloated code. Common practice is to offload this responsibility to development-time and/or build-time tools.

TypeScript and Flow are typed supersets of JS that will let you write in a typed language and whose compilers do type checks for you.

If you prefer vanilla JS, using an editor or IDE that utilizes JSDoc to do basic checks is also an option. I know Visual Studio's Intellisense or Intellij IDEA with the right plugins do this.