eval() vs. Function() in JavaScript Last Updated : 19 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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(); Output[String: '16'] 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 Output16 Difference between eval() vs. Function() in JavaScriptFeatureeval()Function()UsageEvaluates JavaScript code as a stringCreates a new function from a string of codeScopeRuns code in the current lexical scopeCreates a new function with its own lexical scopePerformanceGenerally slower due to parsing and executionCan be faster since code is compiled upfrontSecurityConsidered risky due to potential security issuesGenerally safer as it creates a new function objectDynamic codeSuitable for executing dynamic codeSuitable for generating functions dynamicallySyntax ErrorsMay not throw syntax errors immediatelyThrows syntax errors at creation timeSyntax eval(String) new Function([arg1], functionBody) Comment More infoAdvertise with us Next Article Array of functions in JavaScript A am8254s3a Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions 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 JavaScript Apply() Function The apply() method is used to write methods, which can be used on different objects. It is different from the function call() because it takes arguments as an array. Syntax: apply() Return Value: It returns the method values of a given function. Example 1: This example illustrates the apply() functi 1 min read Like