---
schema_version: "1"
id: b19c7f601f5ba54e8f22915bf48a9e1d3ad90f61027ed25942218efb4a2f2d66
title: eval() Function Is Limited by Lightning Locker
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/lwc/lightning-components-security/guides/locker/locker-js-eval.md
canonical_url: https://developer.salesforce.com/docs/platform/lightning-components-security/guide/locker-js-eval.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 253988d1122ef5572120914efc13338eed425b40c6ebafc5ebd641d940096341
products:
  - Security for Lightning Components
  - lwc
---
# eval() Function Is Limited by Lightning Locker

In Lightning Locker, use of the `eval()` function is supported to enable use of third-party libraries that evaluate code dynamically. However, it’s limited to work only in the global scope of the namespace. The `eval()` function can’t access the local variables within the scope in which it’s called.

Normally, `eval()` has two modes of execution. When you invoke `eval()` directly, it works in the local scope. When you invoke it via a reference, it works in the global scope. Lightning Locker only supports the latter.

For example, suppose that you execute this code:

```javascript
window.foo = 1;
function bar() {
  var foo = 2;
  return eval("foo");
}
```

A call to `bar()` returns 2 when evaluation is performed in the local scope, and returns 1 when it’s performed in the global scope. If you must use variables from the local scope, refactor your code. Use a `Function()`, declare the local variables as parameters, pass them as arguments, and add a return statement:

```javascript
window.foo = 1;
function bar() {
  var foo = 2;
  return Function("foo", "return foo")(foo);
}
```

**tip:** Lightning Web Security doesn’t limit `eval()` to the global scope because the code executes in a sandbox. However, invoking `eval()` directly in local scope is still not recommended.
