Review the given code please

why does the ternary operator does not update the value of title in this case

Not how the title has the comparison operator == instead of the assignment operator =. However, that is the correct way to write the short if statement (x == y ? x : y).

What you are missing is the assignment portion where the result is assigned to a variable: x = x > y ? x : y

Now I understand that
can you please tell me here i am comparing my title value and then adding javascript code in the latter for the ternary operator and yet it isn’t working

import { animals } from ‘./animals’;

import React from ‘react’;

import ReactDOM from ‘react-dom’;

const title = “”;

{ title == “” ? { title = “Click an animal for fun fact”} : {title= “”} }

First, a const variable can’t be updated. It is a constant. Let’s look at this format:

const title = (title == “”) ? "Click an animal for fun fact" : ""

It follows this pattern:
const variable = statement ? true : false

What is assigned to the variable depends on whether the statement is true or false.

brianmonette can you tell me what other forms of variables are there in JSX other than const ,
specifically which can be updated?

Both let and var are used to declare variables which can be updated.

1 Like

Part of learning Javascript is to learn the difference between the types of variables: const, let and var. If you can’t remember, please review this. This is super important to know.

1 Like

You let your ternary be out in the wild. Translated into plain english, that’s what you write: ‘If title is assigned to an empty string ‘Click an animal for fun fact’’.
This string is now floating in the orbit instead of being wrapped by the <h1> tag.
So instead of just repeating <h1>{title}</h1> in the assignment of animalFacts, you should put your ternary in there and render animalFacts rather than <h1>{title}</h1>

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.