In this tutorial, we will learn how to write a function in JavaScript. It will provide a JavaScript eval() function. We learn how to write programs in JavaScript by using the java eval() function. And we learn adding the strings in javascript.
Write the eval()function:
The Syntax is: eval(string)
In JavaScript eval() function is used to evaluate the code.
It executes the code and returns the evaluation result.
The eval() function is useful in critical situations, and security risks.
EXAMPLE 1:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Adding strings</title>
<script>
var str1 = '({"fname1" : "Navya", "lname1" : "Pendyala"})';
var str2 = '({"fname2" : "Swetha", "lname2" : "Pendyala"})';
var obj1 = eval(str1);
var obj2 = eval(str2);
document.write(obj1.fname1 + " " + obj1.lname1 +"<br>");
document.write(obj2.fname2 + " " + obj2.lname2);
</script>
</head>
<body>
</body>
</html>
Output ; Navyasree Pendyala Swetha Pendyala