eval() function with example in JavaScript

JavaScript eval() function: Here, we are going to learn about the eval() function with Example in JavaScript.
Submitted by IncludeHelp, on February 02, 2019

eval() function

eval() function is a predefined global function in JavaScript and it is used to evaluate (execute) an expression, which is passed to the function as a parameter. It can also evaluate any JavaScript code.

Syntax:

    eval(expression);

Here, expression is the JavaScript code or any expression having values or variables.

Example:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>
		var num1 = 100;
		var num2 = 200;
		var num3 = eval("num1 + num2"); //add num1 and num2
		var num4 = eval("num3 + 100"); //add num3 and 100
		var str = eval("num4.toString(16)"); //convert num4 value to hex
		
		document.write("num3: " + num3 + "<br>");
		document.write("num4: " + num4 + "<br>");
		document.write("str: " + str + "<br>");
	</script>
</body>
</html>

Output

num3: 300
num4: 400
str: 190

JavaScript Built-in Functions »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.