DK-Flag Erik Østergaard - Evaluate JavaScript Expression / Evaluere JavaScript udtryk Go to Home Page

Test, check, and debug your JavaScript (JScript) code

Evaluate entry Bottom of This Page

Type in the JavaScript (JScript) expression you want to evaluate in the Input textarea. To see the result, click the "Evaluate" button.

Use only the period (.) as a valid decimal separator.

Evaluate JavaScript Expression



(Geometric Shapes) Evaluate JavaScript ExpressionEvaluate JavaScript Expression

Use this page for testing, checking, and debugging your JavaScript (JScript) code. But first what is the main difference between the programming script languages JavaScript and JScript?

(Geometric Shapes) Evaluate JavaScript ExpressionIn principle

The eval function is a built-in function in JavaScript (JScript) itself, which is not a method (see the Reference information below), and is never applied to an instance using the dot operator (.). It is on the same plane as a function that you create using the function keyword. Although eval( ) sounds as if it should be used only to evaluate mathematical equations, this function can actually do much more. The eval function attempts to evaluate its string argument as a JavaScript (JScript) expression and return its value. All the normal rules for evaluating expressions, including variable substitution, are performed by the eval function. This function is extremely powerful simply because it evaluates any JavaScript (JScript) expression, no matter what that expression does. This means, that you can pass the contents of an HTML (HyperText Markup Language) text field or textarea to the eval function, which promptly tries to execute the contents of that text field or textarea as JavaScript (JScript) code.

(Geometric Shapes) Evaluate JavaScript ExpressionIn practice

Try to type alert('Hello World!') to the "Input:" text field (Input textarea) using the keyboard and then click the "Evaluate" button. You can also try typing true or false to the "Input:" text field (Input textarea) and after each press the "Evaluate" button.

(Geometric Shapes) Evaluate JavaScript ExpressionHow to test a part of your JavaScript (JScript) code

If you isolate and test only a small part of your JavaScript (JScript) code it will make it easier to find possible bugs in your script.

Copy your JavaScript (JScript) code you want to test and debug into the "Input:" text field (Input textarea). Evaluate, test, if necessary make the corrections you want to the code, evaluate again, and so on until you are satisfied and there is no errors in your code and it is working just like you want it to. Finally copy the tested and correct working JavaScript (JScript) code back into your application.

While your are testing you can always use an alert dialog box to show the contents of one or more of your variables while your JavaScript (JScript) code is running.

(Geometric Shapes) Evaluate JavaScript ExpressionExample of testing a part of your JavaScript (JScript) code

If you are programming e.g. a function in JavaScript (JScript) you can use this "Evaluate JavaScript Expression" to test it. Try to type or copy this code to the "Input:" text field (Input textarea):

function doReverseString(inputString) {
  // Function to reverse all characters in a string.
  var newString = '';
  for (var i=(inputString.length - 1); i>=0; i--) {
    newString += inputString.charAt(i);
  return (newString);

doReverseString('This is a test - 1234567890');

Conventions used for: Source code syntax highlighting.

Then click the "Evaluate" button. Afterwards you can also try changing "This is a test - 1234567890" to something else. After each change press the "Evaluate" button. In this way you can easily test and debug only a small part of your code at one time.

or... / eller...

See similar source code for JavaScript (including this example as shown above) here: / Se tilsvarende kildekode for JavaScript (herunder dette eksempel som vist ovenfor) her:

Change Case / Store/små bogstaverOpen this link in new window / Åben dette link i nyt vindue

----- ----- ----- ----- -----

See possibly also relevant pages here: / Se eventuelt også relevante sider her:

(Geometric Shapes) Source code snippets Source code snippets / Kildekode småstykker*)

*) Opens in a new browser window. Just close this new window and you are back. / Åbner i et nyt browser vindue. Luk dette nye vindue og du er tilbage.

Reference information


Specification from December 12, 1996

Method. The eval method evaluates a string of JavaScript code in the context of the specified object.




objectName is the object for which a string is to be evaluated. If omitted, the string is evaluated without regard to any object.

string is any string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

Method of

eval is a method of all objects.

Implemented in

Navigator 2.0: a built-in JavaScript function, not associated with any object, but part of the language itself

Navigator 3.0: a method of every object


The argument of the eval method is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.

If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.


Example 1. Both of the write statements below display 42. The first evaluates the string "x + y + 1," and the second evaluates the string "42."

var x = 2
var y = 39
var z = "42"
document.write(eval("x + y + 1"), "<BR>")
document.write(eval(z), "<BR>")

Example 2. In the following example, the getFieldName(n) function returns the name of the nth form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element.

var field = getFieldName(3) 
document.write("The field named ", field, " has value of ", eval(field + ".value"))

Example 3. The following example uses eval to evaluate the string str. This string consists of JavaScript statements that open an Alert dialog box and assigns z a value of 42 if x is five, and assigns zero to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z.

var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; "
document.write("<P>z is ", eval(str))

Example 4. In the following example, the setValue function uses eval to assign the value of the variable newValue to the text field textObject:

function setValue (textObject, newValue) {
   eval ("document.forms[0]." + textObject + ".value") = newValue

Example 5. The following example creates breed as a property of the object myDog, and also as a variable. The first write statement uses eval('breed') without specifying an object; the string "breed" is evaluated without regard to any object, and the write method displays "Shepherd", which is the value of the breed variable. The second write statement uses myDog.eval('breed') which specifies the object myDog; the string "breed" is evaluated with regard to the myDog object, and the write method displays "Lab", which is the value of the breed property of the myDog object.

function Dog(name,breed,color) {
myDog = new Dog("Gabby")
var breed='Shepherd'
document.write("<P>" + eval('breed'))
document.write("<BR>" + myDog.eval('breed'))

Example 6. The following example uses eval within a function that defines an object type, flintstone. The statement fred = new flintstone("x=42") creates the object fred with the properties x, y, z, and z2. The write statements display the values of these properties as 42, 43, 44, and 45, respectively.

function flintstone(str) {
   this["z2"] = 45
fred = new flintstone("x=42")
document.write("<BR>fred.x is " + fred.x)
document.write("<BR>fred.y is " + fred.y)
document.write("<BR>fred.z is " + fred.z)
document.write("<BR>fred.z2 is " + fred.z2)

   Top of This Page
   Go to Home Page