JavaScript eval() function - JavaScript Tutorial

JavaScript eval() function

BLUF: This tutorial on JavaScript eval() function provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript eval() function

Understanding JavaScript eval() function is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The eval function in JavaScript serves to assess the expression. It is a universal function in JavaScript that assesses the designated string as JavaScript code and runs it.

When using the eval function, it expects a string as its parameter. If the provided parameter consists of statements, eval will assess those statements. In cases where the parameter is an expression, eval will compute the expression. If the input parameter of eval is not a string, the function will simply return the parameter as it is without making any changes.

The eval function has certain constraints, including its disrecommendation due to security concerns. It is advised against due to its impact on speed and code clarity.

Syntax

Example

eval(string)

Values

It receives a sole parameter, which is described in the following manner.

A string in JavaScript serves as a representation of a JavaScript expression, a single statement, or a series of statements. It may denote a variable, statement, or a JavaScript expression.

To grasp the functionality of the JavaScript eval function, we will utilize visual aids.

Example1

This is a basic illustration demonstrating the evaluation of an expression by utilizing the eval function. Within this instance, various variables are employed. The eval function is being used on the variables a, b, and c to determine the total, product, and the result of subtraction.

Example

<html>

<head>

<script>

var a = 10, b = 20, c = 30, 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

Upon running the provided code, the resulting output will be -

Example

60

6000

-10

Example2

In this instance, we demonstrate the invocation of a function utilizing the eval function. Specifically, a function named fun is defined with a pair of parameters and it yields the product of the two provided arguments.

The function is being invoked within the eval function, and the output is being stored in a variable named res.

Example

<html>

<head>

<script>

var res;



function fun(a, b)

{

return a * b;

}



eval("res = fun(50, 50);");

document.write(res);



</script>

</head>



<body>

</body>

</html>

Output

After the execution of code, the output will be -

Example3 - Evaluate the string with JavaScript statements

In this instance, we demonstrate the utilization of the eval function to assess a string containing JavaScript code. The string, named str, comprises a JavaScript conditional statement involving an if-else clause. The evaluation involves checking the value of the variable 'x'. If 'x' equals 0, the result will be 'SUNDAY'; otherwise, it will be 'MONDAY'.

Example

<html>

<head>

<script>

var x = 0;

var str = "if(x == 0) {'SUNDAY'} else 'MONDAY';";



document.write('The output is : ', eval(str));

</script>

</head>



<body>

</body>

</html>

Output

After the execution of code, the output will be -

Example

The output is : SUNDAY

Example - convert string to JavaScript Objects

Here, we are using the object obj to represent the data.

In this scenario, we are utilizing the object labeled as obj to symbolize the data.

Example

<html>

<head>

<script>

var str = '({"fname" : "Harry", "lname" : "Rickman"})';



var obj = eval(str);



document.write(obj.fname + " " + obj.lname);

</script>

</head>



<body>

</body>

</html>

Output

After the execution of code, the output will be -

Example

Harry Rickman

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience