eval() vs. Function() in JavaScript
Last Updated :
19 Apr, 2024
We will learn about
JavaScript functions
eval()
and Function().
The eval()
and Function()
are used to evaluate any JavaScript expression passed to either of them as a string but the difference between them is how how they handle the expression.
eval()
The eval() method in JavaScript evaluates or executes its argument. If the argument is an expression, it evaluates the expression. If it’s one or more JavaScript statements, eval() executes the statements.
Syntax:
eval(String)
Example: In this example, we use `eval()` to evaluate the multiplication expression of `a` and `b`, converting the result to a string.
JavaScript
// JavaScript to illustrate eval() function
function func() {
// Original string
let a = 4;
let b = 4;
// Finding the multiplication
let value = eval(new String(a * b));
console.log(value);
}
// Driver code
func();
Function()
The Function()
constructor creates a new function object dynamically from a string representing JavaScript code. It allows us to define functions programmatically at runtime. The code inside the string is compiled when the function is created, not when it is called.
Syntax:
new Function([arg1, arg2, ...argN], functionBody)
Example: In this example, we use the Function()
constructor to create a new function that multiplies two numbers.
JavaScript
// JavaScript to illustrate Function() constructor
// Define a string containing JavaScript code
const code = 'return 4 * 4;';
// Create a new function using Function() constructor
const multiply = new Function(code);
// Execute the dynamically created function
const result = multiply();
console.log(result); // Output: 16
Difference between eval() vs. Function() in JavaScript
Feature | eval() | Function() |
---|
Usage | Evaluates JavaScript code as a string | Creates a new function from a string of code |
---|
Scope | Runs code in the current lexical scope | Creates a new function with its own lexical scope |
---|
Performance | Generally slower due to parsing and execution | Can be faster since code is compiled upfront |
---|
Security | Considered risky due to potential security issues | Generally safer as it creates a new function object |
---|
Dynamic code | Suitable for executing dynamic code | Suitable for generating functions dynamically |
---|
Syntax Errors | May not throw syntax errors immediately | Throws syntax errors at creation time |
---|
Syntax
| eval(String)
| new Function([arg1], functionBody)
|
---|
Similar Reads
JavaScript eval() Function The eval() function in JavaScript is a powerful but potentially dangerous feature that allows the execution of JavaScript code stored in a string. While eval() can be useful in some cases, its use is generally discouraged due to security risks and performance concerns.Executing JavaScript Code with
2 min read
Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9)); // output: 15Function Syntax
5 min read
Arrow functions in JavaScript An arrow function is a shorter syntax for writing functions in JavaScript. Introduced in ES6, arrow functions allow for a more concise and readable code, especially in cases of small functions. Unlike regular functions, arrow functions don't have their own this, but instead, inherit it from the surr
5 min read
JavaScript Function Call The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). This allows borrowing methods from other objects, executing them within a different context, overriding the default value, and passing arguments. Syntax: cal
2 min read
Array of functions in JavaScript Given an array containing functions and the task is to access its element in different ways using JavaScript. Approach: Declare an array of functions.The array of functions works with indexes like an array function. Example 1: In this example, the function call is initiated from the element of the a
2 min read