Home » JavaScript

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.