# Can you use eval() in p5js?

**URL:** <https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183>\
**Category:** Beginners\
**Created:** [December 18, 2021, 11:40am UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183 "2021-12-18T11:40:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [December 18, 2021, 11:40am UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/1 "2021-12-18T11:40:02Z")

</div>

is the eval() function available for p5js?

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 18, 2021, 11:46am UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/2 "2021-12-18T11:46:34Z")

</div>

Yes. See:

> **[eval() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)**
>
> The eval() function evaluates JavaScript code represented
> as a string.

It is available in JavaScript, and works in p5.js.

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [December 18, 2021, 11:53am UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/3 "2021-12-18T11:53:13Z")

</div>

for future reference, are there any javascript functions that are not in p5.js?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 18, 2021, 12:00pm UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/4 "2021-12-18T12:00:12Z")

</div>

p5.js is a JS library for browsers (frontend).

You can look up here for what’s available in JS out-of-the-box:

> **[JavaScript reference - JavaScript | MDN](https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference)**
>
> This part of the JavaScript section on MDN serves as a repository of facts about the JavaScript language. Read more about this reference.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 18, 2021, 12:10pm UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/5 "2021-12-18T12:10:23Z")

</div>

An online p5.js + dat.gui sketch using **eval()**:

> **[Pulsing Equations](http://Bl.ocks.org/gosubroutine/f9a0a94dcfee23a037b35f308b256d98)**
>
> GoSubRoutine’s Block f9a0a94dcfee23a037b35f308b256d98

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 18, 2021, 12:21pm UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/6 "2021-12-18T12:21:08Z")

</div>

> [@MoonAlien822](#):
>
> … are there any javascript functions that are not in p5.js?

That’s an interesting question. Since when we are using the p5.js library, we are using JavaScript, all the JavaScript functions could potentially be available to us. However, in JavaScript, anyone can overwrite names, including the names of functions.

In the following, `eval` has been overwritten:

```auto
function setup() {
  createCanvas(89, 55);
  textAlign(CENTER, CENTER);
  textSize(24);
  noLoop();
}

function draw() {
  text(eval(7), width / 2, height / 2);
}

function eval(n) {
  // redefine the JavaScript eval function
  return n * n * n;
}

```

Image:  
 ![343](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6f0a015ceb1ab3f12101f3910444a56f324f01df.png)

So, to answer your question, we could check all of the p5.js library documentation to see whether any JavaScript function names have been redefined. Perhaps someone here already knows whether that is the case.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 18, 2021, 12:37pm UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/7 "2021-12-18T12:37:16Z")

</div>

> [@javagar](#):
>
> Perhaps someone here already knows whether that is the case.

That is the case for the p5::**print()** method, which overrides Window::**print()**:

> **[reference | p5.js](https://p5js.org/reference/#/p5/print)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

> **[Window: print() method - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/print)**
>
> Opens the print dialog to print the current document.

So besides [JS builtins](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) we also have Window methods available in the global context when using a browser (frontend):

> **[Window - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window)**
>
> The Window interface represents a window containing a DOM document; the document property points to the DOM document loaded in that window.

---

<div class="post-metadata">

**Author:** ![KumuPaul](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kumupaul/32/13072_2.png) [@KumuPaul](https://discourse.processing.org/u/KumuPaul)\
**Post date:** [December 18, 2021, 9:35pm UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/8 "2021-12-18T21:35:58Z")

</div>

> [@GoToLoop](#):
>
> That is the case for the p5:: **print()** method, which overrides Window:: **print()**

However, the implementation of the p5.js `print()` function preserves the behavior of the global built in function by calling the original function when no arguments are specified:

```javascript
_windowPrint = window.print;
p5.prototype.print = function(...args) {
  if (!args.length) {
    _windowPrint();
  } else {
    console.log(...args);
  }
};

```

---

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [December 23, 2021, 8:26am UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/9 "2021-12-23T08:26:19Z")

</div>

I thought eval() was retired years ago because of being a dangerous function, as an attack vector.

Years avoiding it and suddenly I find it is still available.

Does it mean you can program a worksheet in js/p5js?

---

<div class="post-metadata">

**Author:** ![KumuPaul](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kumupaul/32/13072_2.png) [@KumuPaul](https://discourse.processing.org/u/KumuPaul)\
**Post date:** [December 23, 2021, 8:24pm UTC](https://discourse.processing.org/t/can-you-use-eval-in-p5js/34183/10 "2021-12-23T20:24:05Z")

</div>

`eval()` was never “retired” it’s just that its use is discouraged because 1) it is very easy to create a security vulnerability when using it, and 2) there are plenty of things for which one could use `eval()` but which have better solutions (both in terms of performance and security).

If by “worksheet” you mean a spreadsheet like program, then certainly, this would be possible with or without `eval()`. In order to support formulas in a spreadsheet you can either write an interpreter to process formulae, or you can write some code that transpiles/sanitizes formulae into JavaScript which can be executed with `eval()`. But what you absolutely would not want to do is simply `eval()` user input as JavaScript. Technically there’s nothing wrong with doing this if user A only ever runs user A’s JavaScript (although they could write some JavaScript that would crash your page which would be a bad user experience), however, what would be very bad would to let user A write some JavaScript that you then `eval()` in user B’s browser (say because you allow users to save and share spreadsheets). This would let user A do things like steal user B’s credentials or personal information, inject ads into user B’s page, or to use user B’s browser tab for nefarious purposes.
