The role of JS Eval ()

Source: Internet
Author: User

1. Calculating strings

Example: a formula for a string type----a pure number

1 var str = ' 1 ' + ' 2 ' + ' 3 '; 2 var count = eval (str); 3 document.write (count); // at this point count will be calculated as 6.

Another example: the mix of characters and numbers

var code = ' x ' + 1; var count = eval (code);d ocument.write ("count=" +count); // count=x1

2. Convert a string to an array/json/object

This is because the JSON string that is returned cannot be parsed as an array when the project is written, so it can be traversed like an array by using the eval transform.

var array = eval ("[" +data+ "]"); // at this point the conversion for array // But whether it can be converted to exactly the desired array depends on the specifics of the returned data, so be sure to remember the output to see if it satisfies var object = eval (' (' + str+ ') '); // convert to an object var json = eval (' {' +str+ '} '); // Convert to JSON // this and the previous conversion to the object, should be the same  when used, please test, depending on the situation

3. Execute JavaScript code.

var s= ' ss 'function Fun () {eval (' var s= ' xx ');   Here is the equivalent of var s = "xx";  // output SS

Reference: http://www.jb51.net/article/30008.htm
Only as a note in the collation here, not fully written and incorrect, please advise ~ ~

The role of JS Eval ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.