# Simple js arrow funtion and ternary operator question

**URL:** <https://discourse.processing.org/t/simple-js-arrow-funtion-and-ternary-operator-question/38993>\
**Category:** Beginners\
**Created:** [September 27, 2022, 5:29pm UTC](https://discourse.processing.org/t/simple-js-arrow-funtion-and-ternary-operator-question/38993 "2022-09-27T17:29:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [September 27, 2022, 5:29pm UTC](https://discourse.processing.org/t/simple-js-arrow-funtion-and-ternary-operator-question/38993/1 "2022-09-27T17:29:12Z")

</div>

What I’m doing wrong with my atempt to use an arrow function here?

```auto
function setup() {
  createCanvas(400, 400);
  const x = 0;
  const y = 10;
  const t1 = x > y ? x : () => console.log("nono from arrow");
  const t2 = x > y ? x : nono();
}

function nono() {
  console.log("nono from func");
}

//this code logs: 'nono from func' 

```

thanks

---

<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:** [September 27, 2022, 5:38pm UTC](https://discourse.processing.org/t/simple-js-arrow-funtion-and-ternary-operator-question/38993/2 "2022-09-27T17:38:49Z")

</div>

> [@vkbr](#):
>
> `const t1 = x > y ? x : () => console.log("nono from arrow");`

On the statement above you’re conditionally creating/defining a function.

> [@vkbr](#):
>
> `const t2 = x > y ? x : nono();`

And on the 1 above you’re conditionally calling/invoking an already existing function.

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [September 27, 2022, 5:42pm UTC](https://discourse.processing.org/t/simple-js-arrow-funtion-and-ternary-operator-question/38993/3 "2022-09-27T17:42:10Z")

</div>

Ok, that makes sense. Thanks.  
So what I was looking to do was

```auto
 const t1 = x > y ? x : (() => console.log("nono from arrow"))();

//that logs: 
//nono from arrow 
//nono from func  

```

The so talked about self-invoking anonymous function at least! Great! thanks

---

<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:** [September 27, 2022, 5:43pm UTC](https://discourse.processing.org/t/simple-js-arrow-funtion-and-ternary-operator-question/38993/4 "2022-09-27T17:43:25Z")

</div>

> **[IIFE - MDN Web Docs Glossary: Definitions of Web-related terms | MDN](https://developer.mozilla.org/en-US/docs/Glossary/IIFE)**
>
> An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.
> The name IIFE is promoted by Ben Alman in his blog.

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [September 27, 2022, 6:18pm UTC](https://discourse.processing.org/t/simple-js-arrow-funtion-and-ternary-operator-question/38993/5 "2022-09-27T18:18:11Z")

</div>

Wrong again LoL  
thanks :)))
