Jump to content

Eval()


Chocolate570

Recommended Posts

Well, i was making a calculator and was having trouble with the + operator because the input was from a text box, making it a string. I went to w3schools, and found the eval function. It worked. :) But i want to know, what happens if you throw letters at it? I tried using it for a thing that stops any letters in the text box at the top of the calculator...but it didn't work. I found out how to do that, but what does it do?Thanks in Advance.Choco

Link to comment
Share on other sites

Think of it as if it didn't have eval around it at all - so if you've got:3 + 6 + youit's going to error, unless you have a variable called you. It's a useful thing for converting text to javascript statements, like:

<script type='text/javascript'>var t = 8var i = eval("3+5+t");alert(i);</script>

If you can understand that.

Link to comment
Share on other sites

Yup. Unless you had a variable called abc.

eval() is a function to evaluate a piece of javascript code, like so:
eval("alert('Hello')");

Notice two things:1: The code must be inside quotes ( "" )2: The code inside the quotes cannot end with a semi-colon ( ; ), which means you can only evaluate 1 line of code at a time.I had to use eval() when I made my own game... :):)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...