Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Using the Java Script eval() Function

The Java Script eval() function accepts a string argument. If the string is an expression, i.e. numbers (may be literal or variable names) and operators that evaluates to a single value, eval() evaluates the expression. If the string contains JavaScript statements, i.e. commands or functions, eval() executes the statements.

Shown below is the syntax of the eval() function:


Shown below is an example of code that uses eval() to evaluate an expression:

<script type="text/javascript">

var num = 3;
var res = eval(num * 4);


You might think that you could easily code a calculator by submitting the contents of a form text box directly as an argument to the eval function, but this would not be a good idea because eval also executes the statements. For example the XMLHttpRequest function used in Ajax development gives access to the server.

Shown below is an example of code that uses eval() to execute statements:

<script type="text/javascript">

eval("min=0; max=3; document.write(Math.floor(Math.random() * (max - min + 1) + min))");


In the code above, a string containing the Math objects .random() function and .floor() function, and the document's .write() function is passed to the eval() function. The eval() function executes the code in the string which generates a random number between 0 and 3 and writes it to the document.

More Java Script Code:
• JavaScript Operators
• Access Web Page Elements With getElementById
• Interactively Set Webpage Colors
• The switch / case Structure
• Using the Java Script eval() Function
• Dueling Windows
• The Navigator Object
• Java Script Data Types
• The do/while Loop
• A Brief History of JavaScript

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2017 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268