JavaScript eval() function is used to evaluate the expression. It is the JavaScript global function that evaluates and executes the provided text as JavaScript code.
Table of Contents
JavaScript eval() function: Key Points
- The eval() function takes a string as its argument.
- eval() evaluates the statements if the parameter reflects them.
- If the parameter is an expression, eval() will evaluate it. If the parameter to eval() is not a string, the function returns it unmodified.
- The eval() function has various restrictions, such as that it is not advised for usage for security reasons. It is not recommended since it slows down the process and makes the code illegible.
JavaScript eval() function Syntax
eval(string)
JavaScript eval() function Parameter Values
Parameter | Description |
string | A JavaScript expression, statement, variable, or sequence of statements |
JavaScript eval() function Examples & Illustrations
Let’s look at a few examples to grasp the JavaScript eval() method better.
Example1: JavaScript eval() function to evaluate an expression
It is a basic example of using the eval() function to evaluate an expression. There are some variables in this example. To compute the sum, multiplication, and subtraction, we use the eval() function on variables a, b, and c.
<html> <head> <script> var a = 1, b = 2, c = 3, sum, mul, sub; sum = eval(" a + b + c "); mul = eval(" a * b * c"); sub = eval(" a - b"); document.write(sum + "<br>"); document.write(mul + "<br>"); document.write(sub); </script> </head> <body> </body> </html>
Output
After the execution of the above code, the output will be –
Example2: JavaScript eval() function to invoke a function
In this example, we’re utilizing the eval() method to invoke a function. Fx() is a function with two arguments that returns the multiplication of both parameters.
The function is called in the eval() function, and the result is saved in the res variable.
<html> <head> <script> var re; function fx(a, b) { return a * b; } eval("re = fx(20, 20);"); document.write(re); </script> </head> <body> </body> </html>
Output
After the execution of the above code, the output will be –
Example 3: Use JavaScript eval() function to evaluate the string
To evaluate the text using JavaScript statements, we use the eval() method in this example. We are matching the value of the variable ‘x’; if x is 0, the output is ‘Pizza’; otherwise, the result is ‘Sushi’ There is a string str with a JavaScript conditional if-else expression in this case.
<html> <head> <script> var x = 0; var str = "if(x == 0) {'PIZZA'} else 'SUSHI';"; document.write('The output is : ', eval(str)); </script> </head> <body> </body> </html>
Output
After the execution of the above code, the output will be –
Example 4: Use JavaScript eval() function to convert a string to JavaScript Objects
In this last example, we’re going to convert a string to a JSON object. In this case, the string str holds the data rather than the code. We must convert the data to JSON, which allows the string to express the data using a subset of JavaScript syntax.
Here, ob is used to represent data.
<html> <head> <script> var str = '({"fname" : "Jones", "lname" : "Douglas"})'; var ob = eval(str); document.write(ob.fname + " " + ob.lname); </script> </head> <body> </body> </html>
Output
After the execution of the above code, the output will be –
JavaScript eval() function Browser Support
Browser Support | Chrome | Edge | Safari | Mozilla Firefox | Opera |
Function | |||||
eval() | Yes | Yes | Yes | Yes | Yes |
You ought to take part in a contest for one of the greatest blogs on the web. Im going to highly recommend this site!