
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Explain JavaScript eval() function what are the rules to be followed while using it.
The eval() function is used to evaluate the given string and execute it as JavaScript code. Using eval() is highly dangerous as someone can pass malicious code as input string to one of out inputs.
Following is the code to show eval() function in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>JavaScript eval() function</h1> <div style="color: green;" class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to see eval() usage</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); function multiply(a, b) { return a * b; } BtnEle.addEventListener("click", () => { resEle.innerHTML += eval("12+92") + ""; eval('resEle.innerHTML += "Hello world "'); resEle.innerHTML += eval("multiply(12,33)"); }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- What are the rules to be followed while using varargs in java?
- What are the best practices to be followed while using JavaScript?
- Rules to be followed while making Investment Decisions
- What are the rules to be followed while making a variable static and final?
- What are the rules to be followed for Object Definitions in JavaScript?
- What are the rules to be followed while working with a switch statement in java?
- What are the guide lines to be followed while using wildcards in Java?
- What is the use of JavaScript eval function?
- eval() function in JavaScript
- Why is using the JavaScript eval() function a bad idea?
- What are the rules to follow while using exceptions in java lambda expressions?
- Guidelines to be followed while implementing equals method in Java?
- What is the difference between void, eval, and the Function constructor in JavaScript?
- What are the basic rules for JavaScript parameters?
- eval() function in PHP

Advertisements