![]()
Erik Østergaard - Evaluate JavaScript Expression / Evaluere JavaScript udtryk
|
|
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.
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?
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.
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.
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.
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.
[objectName.]eval(string)
"3 * x + 2", to a variable, and then calling eval at a later point in your script.
var x = 2Example 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 y = 39
var z = "42"
document.write(eval("x + y + 1"), "<BR>")
document.write(eval(z), "<BR>")
var field = getFieldName(3)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.
document.write("The field named ", field, " has value of ", eval(field + ".value"))
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) {
this.name=name
this.breed=breed
this.color=color
}
myDog = new Dog("Gabby")
myDog.breed="Lab"
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.eval("this."+str)
this.eval("this.y=43")
this.z=44
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)
©1997 - 2008 Erik Østergaard, Copenhagen, Denmark.