javascript Eval function with examples in javascript

In this blog post, We are going to learn the javascript eval function with examples.

javascript eval function

eval is a predefined global function in javascript.

It executes String in the form of Javascript code. It takes the input as a string and returns the output of the javascript expression code string. The code can be variables, objects, and objects which javascript executes using this function.

This function works in javascript and Typescript as typescript is a superset of javascript programming language.

Syntax:

eval(code);

parameter code: is a javascript expression with single/multiple statements. These expressions are enclosed in single quotes for expression. It executes the expression as a javascript statement,

For example, if code contains statement arithmetic expressions, It evaluates arthematic expressions.

Notes:

  • Javascript is client-side, passing malicious code from chrome debugger makes it less vulnerable to security issues.
  • Modern javascript interpreters convert to machine code, user input contains variable passed, eval will force the browser to find out where variable exists on machine code and expensive operation.
  • if you are passing user input into the eval function, There is no danger to it.
  • if a user passes malicious code injected from the client-side and the possibility of an attacker stealing information, Hence It is not advisable to use

The following are the different examples of using the eval() function.

Eval arithmetic expressions in javascript

This is a simple arithmetic expression code that we can pass to the eval function.

It evaluates the expression as javascript code at runtime and returns the output.

var temp = 6;
console.log(eval("temp + 51")); // output is 57
var temp = 5;
alert(eval("(x + 4) - 2")); // output is 7

How to Create and call a Function using eval dynamically

In this, Create a javascript function and call using eval expression.

The below code creates the doubleFunction function dynamically.

Created function declaration code enclosed in a string. And string representing a function is called using the eval function. Finally, call the dynamic function a normal function.

var strDoubleFunction =
  "function doubleFunction(parameter) { return parameter* parameter; }";
eval(strDoubleFunction);
console.log(doubleFunction(5)); // 25
console.log(doubleFunction(4)); // 16

How to declare variable strings using eval?

In this example, we can declare multiple javascript statements dynamically Here, variable through eval is being used and variable exists like a javascript code.

here is an code for javascript statements eval example

var strStatements = "var t1=20;var t2=30;";
eval(strStatements);
console.log(t1); // 20
console.log(t2); // 30

How to Convert String to JSON using eval function?

if eval function contains a string which is of JSON data.

var strJson = '({"id":"1","name":"cloudhadoop"})';
var jsonObj = eval(strJson);
console.log(jsonObj.id); // 1
console.log(jsonObj.name); // cloudhadoop

However, it is always advisable to use the JSON.parse() function to convert string to JSOn.

Eval with Local Context and Global Context

By default without a window object, It will create a Local Context. In the local context, It will create variables and objects in the local scope only.

For using a Global scope, You can use a window object with an eval function like window.eval() eval function is evil and dangerous eval evaluates the javascript code in the form of a string in the browsers. Please always be careful to use this function as it will cause invalid code injected into it, not securable.

performance of this code is very less and not readable.