> ## Content Index
> Fetch the complete content index at: https://www.bramadams.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# A Chat with GPT-4 : A Tangle With Ternary
- URL: https://www.bramadams.dev/202303151637/
- Published: 2023-03-15T20:50:21.000Z
- Updated: 2023-03-17T00:05:18.000Z
- Description: What did GPT-4 teach me about using the ternary operator in JavaScript?
- Author: Bram Adams
- Tags: chatgpt, programming

I had a sneaky bug in my JS logic trying to take advantage of the `undefined || fallback` trick. What I failed to consider was that by using `|| true` the statement would **always** evaluate to true. Luckily, GPT-4 straightened me out!

![https://bram-adams.ghost.io/content/images/2023/03/ternary-bug.png](https://bram-adams.ghost.io/content/images/2023/03/ternary-bug.png)

ternary bug.png

```
---
system_commands: ['I am a generalist that is not afraid to make radical associations between distinct topics by using creative leaps. I write in short, quippy sentences that has a conversational tone.']
temperature: 0.0
top_p: 1
max_tokens: 512
presence_penalty: 1
frequency_penalty: 1
stream: true
stop: null
n: 1
model: gpt-4
---

```

what happens to `stream: metaMatter?.stream || this.settings.stream || true` if settings.stream is false

---

**GPT:**

If `settings.stream` is false, then the conditional statement `metaMatter?.stream || this.settings.stream || true` will evaluate to `true`. This means that the value of `stream` will be set to `true`, regardless of whether or not there is a value for `metaMatter.stream`.

---

**Bram:**

what if i change the last or to and

---

**GPT:**

If you change the last `||` to `&&`, then the value of `stream` will be set to `false`. This is because the conditional statement `metaMatter?.stream || this.settings.stream && false` will first evaluate whether there is a value for `metaMatter.stream`. If there isn't, it will move on to the next condition and check whether `this.settings.stream` is true or false. Since we've changed the last operand from true (`|| true`) to false (`&& false`), it means that even if `this.settings.stream` is true, it won't matter as long as there's no value for `metaMatter?.stream`, and hence, stream would be set to false.

---

**Bram:**

if metaMatter?.stream is undefined what happens

---

**GPT:**

If `metaMatter?.stream` is undefined, the conditional statement `metaMatter?.stream || this.settings.stream || true` will move on to evaluate the next condition which is `this.settings.stream`. If `this.settings.stream` is false (because you said above that it's false in this scenario), then the final expression will be `(undefined || false || true)`, which evaluates to `true`. As a result, regardless of whether or not there's a value for `metaMatter?.stream`, stream would still be set to true.

---

**Bram:**

can you write a ternary using the following rules (lower number == higher priority):

1. stream can be undefined in frontmatter or be in file and be true or false
2. if stream is undefined fallback on global settings toggle of stream value
3. if toggle is undefined fallback on true

---

**GPT:**

Sure, here is a ternary that satisfies the rules you've mentioned:

```
const stream = metaMatter?.stream !== undefined
  ? metaMatter.stream // If defined in frontmatter, use its value.
  : this.settings.stream !== undefined
    ? this.settings.stream // If not defined in frontmatter but exists globally, use its value.
    : true; // Otherwise fallback on true.

```

The above ternary checks if `metaMatter?.stream` is not equal to `undefined`. If it's not, it uses the value of `metaMatter.stream`. But if it is undefined, then we check whether there's a value for `this.settings.stream`. If there is one and it's either true or false (not undefined), we'll use that. Finally, if all other conditions fail (i.e., both `metaMatter?.stream` and global toggle are undefined), then we set stream to true as per rule #3.

---

**bramadams.dev is a reader-supported [published Zettelkasten](https://www.bramadams.dev/about/). [Both free and paid subscriptions are available](#/portal). If you want to support my work, the best way is by taking out a paid subscription.**