Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow Function Not Working In Ternary Operator

My arrow function does not work inside a ternary operator and also it gives me no error in the console

Below Is My Code

The Objects in the Holders Array Are Html Elements

const Holders =
    [
        {
            name: accountHolder,
            target: accountHelper
        },
        {
            name: helpHolder,
            target: helpContainer
        }
    ]
const holderbg = 'rgba(128,128,128,.1)'
document.querySelector('html').onclick = (event) => {
    Holders.map(holder => {
        holder.name.getAttribute('role') == event.target.getAttribute('role') ?
            // The Arrow Function Does Not Work But The Code Runs When It is not in the function
            // replacing ()=>{} with just holder.name.style.backgroundColor = holderbg works 
            () => {
                holder.name.style.backgroundColor = holderbg
                holder.target.classList.remove('hide')
            }

            :
            // The Arrow Function Does Not Work But The Code Runs When It is not in the function
            // replacing ()=>{} with just holder.name.style.backgroundColor = 'initial' works 
            () => {
                holder.name.style.backgroundColor = 'initial'
                holder.target.classList.add('hide')
            }


    })

}

like image 514
Freduah Gideon Avatar asked Feb 02 '26 01:02

Freduah Gideon


2 Answers

You have to execute the function you declared:

(() => {
            holder.name.style.backgroundColor = holderbg
            holder.target.classList.remove('hide')
        })()
like image 179
monkey-0001 Avatar answered Feb 04 '26 14:02

monkey-0001


This is due to the fact that you are just declaring the arrow functions here, they are not being called anywhere.

To call them, you can store the returned function in variable then call it.

document.querySelector('html').onclick = (event) => {
    Holders.map(holder => {
        const func = holder.name.getAttribute('role') == event.target.getAttribute('role') ?
            () => {
                holder.name.style.backgroundColor = holderbg
                holder.target.classList.remove('hide')
            }

            :
            () => {
                holder.name.style.backgroundColor = 'initial'
                holder.target.classList.add('hide')
            }

        func() // called here


    })

}

In the above code, the references to the functions will be created in every event call. So better way is to save the function in variables like this

const show = () => {
    holder.name.style.backgroundColor = holderbg
    holder.target.classList.remove('hide')
}

const hide = () => {
    holder.name.style.backgroundColor = 'initial'
    holder.target.classList.add('hide')
}

document.querySelector('html').onclick = (event) => {
    Holders.map(holder => {
        holder.name.getAttribute('role') == event.target.getAttribute('role')
            ? show()
            : hide()

    })

}

Side note - the map function used in the code is not being used as its application because map returns a transformed array according to the function passed. In this code, the function is returning undefined and also the result is not being stored. Consirder replacing it with forEach

like image 24
Shivam Singla Avatar answered Feb 04 '26 14:02

Shivam Singla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!