JavaScript Calculator

v.2.0

Simplest Calculations:

42
Press Ctrl+Enter; output: 42
2 * 2
output: 4
x = 256 // in strict mode, should be: var x = 256
x/2
output: 128
pow(3, 4) + 1 // no need to prepend with Math.
// in strict mode, should be:
Math.pow(3, 4) + 1
output: 82

Additional Functions:

bin(numeric)
returns a string with binary representation of number

hex(numeric, upperCase)
returns a string with a hexadecimal representation of number

f2c(numeric)
converts Fahrenheit degrees to Celsius degrees

c2f(numeric)
converts Celsius degrees to Fahrenheit degrees

String.format(arguments…)
example:
"{0} * {1} = {2}".format(7, 4, 28)
returns: 7 * 4 = 28

The function property format is hidden from the iterators, as well as Object.dump.

Math Functions:

Math.* functions don't need to be prefixed with Math. Example:

var value = exp(acos(-1))

For strict mode, Math. should be used:

var value = Math.exp(Math.acos(-1))

Console Functions:

If one of these functions is called, an additional console view is shown; all output goes to the console, except for the return value which always goes to the text input line at the bottom. In case of exceptions in the code written in the input text area, exception information is written to the console. When possible, the input text area caret is positioned at the point where the problem is.

write(arguments…)
writes arguments to console, comma-delimited

writeLine(arguments…)
writes arguments to console, comma-delimited, and starts a new line

Object.dump(someName, showFunctionBodies, maxRecursionLevels)
example:
[1, 2, 3].dump("array")
console displays:
array:
    0: [1]
    1: [2]
    2: [3]
end array
and this example:
var value;
({
   a: 1,
   b: PI,
   big: 1/0,
   number: 0/0,
   value: value,
   nothing: null,
   action: function(a, b, c) { return -1; },
   comment: "no comment"
}).
dump ("my object")
console displays:
my object:
    a: [1]
    b: [3.141592653589793]
    big: [Infinity]
    number: [NaN]
    value: [undefined]
    nothing: [null]
    action: [function (a, b, c)]
    comment: ["no comment"]
end my object

All three arguments are optional.

First arguments gives some name to the top object shown in the dump. If omitted, default string representation is used.

If the argument showFunctionBodies is specified and evaluates to true, function bodies (the part in figure brackets {}) are also shown.

If the argument maxRecursionLevels is specified, it allows to dump object recursively. By default, this parameter is 1, that is, the dump is non-recursive. Any number argument greater than 1 specifies the maximum number of levels shown in the dump. Note that even without the limitation of maximum number of levels the recursion does not continue if the object contains circular references. All objects referenced more than once are detected and shown only once, with some []… placeholders shown when an object is referenced again below its first reference. To show the dump with unlimited number of recursion levels, a special object unlimited should be passed. For all other values passed as maxRecursionLevels, the value of 1 is assumed.

Unlimited dump recursion should be used with great care. Remember that all the textual information representing some object graph at once in the output console take considerably greater amount of memory than the source object graph itself. Really big object graphs (notably, document), when represented without limitations to maximum levels of recursions, can hang a browser for a really long period of time, deplete memory and even temporarily damage the browser process. It's recommended to start with low values of the argument maxRecursionLevels (2-4), trying to increase it when it is really needed. Recursive dump is provided as a debug tool, which is not really suitable for routine calculations.

The function property dump is hidden from the iterators, as well as String.format.

Strict Mode

The calculator can dynamically switch to JavaScript strict mode, "use strict"; and this mode is applied to the user code. Please see the check box in the top right corner. Strict mode imposes a number of limitations useful for writing better maintainable, safer code. With this calcularor, in strict mode, prefix Math. should be used.

See also: strict mode.