0

the below code giving some unexpected results. can anyone tell me the reason or logic behind that?

<HTML><body><script type="text/javascript">s="10+10";alert("here 3 : "+eval(s+s+s));alert("here 4 :+eval(s+2));</script></body></HTML>

in first alert it's giving 2040 and in second alert it's giving 112 i'm not able to understand the logic kindly help me.

0

1 Answer 1

1

eval is bad practice, howerer to answer your question:

`s="10+10"`

s is now "10+10"

eval(s+s+s) =>

eval("10+10"+"10+10"+"10+10") =>

eval("10+1010+1010+10") =>

2040

and:

eval(s+2) =>

eval("10+10"+2) =>

eval("10+102") =>

112


what you might have meant is:

eval(s+"+"+s+"+"+s)

and

eval(s+"+2")

Sign up to request clarification or add additional context in comments.

1 Comment

Eval is not necessarily bad, it's just that in the early days of scripting it was abused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.