View Truthful View Source on GitHub


October 9, 2015

In many circuits and formal logic courses, it's useful to be able to quickly see what a truth table for a Boolean expression is. I made a parser and evaluator tool to make that process simpler.

This is a tool to enter boolean expressions and generate truth tables.

Using Truthful.js

Fork the repo and set up dependencies:

npm install
npm install -g bower
bower install

Include the Javascript file in your HTML:

<script src="Truthful.js"></script>

...or in Node:

var Truthful = require("./js/Truthful.js");


Create an algebraic expression like this:

var expression = Truthful.expression("!(b|c|a)");

Expressions can use & for AND, | for OR, => for IMPLIES, <=> for IFF, ! for NOT and round brackets for grouping. Literals true and false may also be used.

The precedence of operators is () > ! > & > | > => > <=>.

To get the result of an expression, pass in an object with values for each variable:

	"a": true,
	"b": false,
	"c": true,
// Returns true

If a variable's value is not specified, it defaults to false.

Expressions can return an object with keys for the variables present in it (rather than an array, so that each variable only can occur once):

// Returns {a:true,b:true,c:true}

Expressions can also render string versions of themselves with brackets specifying order of operations explicitly:

// Returns "(a|(b&(!c)))"

Truth Tables

Get an HTML <table> element of a truth table by passing an input string:

var table = Truthful.truthTable("a => !(b|c|a)");

A string containing an ASCII table formatted for George can also be returned:

Returns a string containing a table such as:
 a | b || a|b
 T | T ||  T 
 T | F ||  T 
 F | T ||  T 
 F | F ||  F

To compare multiple functions, separate each with a comma. A function can be named by optionally specifying a name: beforehand:

Truthful.truthTable("a => !(b|c|a), thing: a <=> b").george()
 a | b | c || a => !(b|c|a) | thing 
 T | T | T ||       F       |   T   
 T | T | F ||       F       |   T   
 T | F | T ||       F       |   F   
 T | F | F ||       F       |   F   
 F | T | T ||       T       |   F   
 F | T | F ||       T       |   F   
 F | F | T ||       T       |   T   
 F | F | F ||       T       |   T   

Error handling

If there is an error with user input, Truthful will throw a new Error() with a useful message. If using Truthful for arbitrary user data, it is recommended to parse input in a try { } catch (err) { } and show the user err.message.


For more details, see GitHub's contribution guide.