Sunday, November 4, 2018

Evil Eval function with examples in javascript | Typescript

In this blog post, We are going to learn  javascript/typescript predefined function eval() with examples.
eval function in javascript and typescript with examples

eval() function - Javascript|Typescript

eval is a predefined global function in javascript. It executes String in the form 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 or single/multiple statements. These expressions are enclosed in single quotes if the parameter is an expression, It executes the expression if the parameter is of javascript statements, it executes the parameters of the statement are arithmetic expressions, It evaluates the arithmetic expressions.

Examples

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

Javascript script code arithmetic string evaluates

This is a simple arithmetic expression code which we can pass with eval function. expressions are evaluated 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

Dynamically Create and call a Function using eval 

The below code creates doubleFunction function dynamically. Created function declaration code in the form of a string and used eval function with string. Finally, call the dynamic function like normal function
var strDoubleFunction = "function doubleFunction(parameter) { return parameter* parameter; }";
eval(strDoubleFunction);
console.log(doubleFunction(5)); // 25
console.log(doubleFunction(4)); // 16

javascript Statements declaration using eval 

In this example, we can declare multiple javascript statements dynamically
var strStatements = "var t1=20;var t2=30;";
eval(strStatements);
console.log(t1); // 20
console.log(t2); // 30

Eval JSON parses - Convert String to JSON 

if eval function contains a string which is of JSON data. it always advisable to use JSON.parse() function.
var strJson = '({"id":"1","name":"cloudhadoop"})';
var jsonObj = eval(strJson);
console.log(jsonObj.id)// 1 
console.log(jsonObj.name)// cloudhadoop 

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 local scope only For using a Global scope, You can use window object with eval function like window.eval()
eval function is evil 
eval evaluates the javascript code in the form of string in the browsers. Please always 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

Related article


EmoticonEmoticon