Saturday, November 02, 2024

eval(), exec() in Python, JavaScript

eval - Wikipedia


Python exec() Function

The exec() function executes the specified Python code.

The exec() function accepts large blocks of code, unlike the eval() function which only accepts a single expression

x = 'name = "John"\n print("Hello, " + name)'
exec(x)

x = 'print("2+3=", 2+3)'
eval(x)

Python Built-in Functions


JavaScript eval() Method

eval() - JavaScript | MDN

console.log(eval('2 + 2'));

// Expected output: 4


No comments: