Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
Function as a callback reloads without respecting the event listener while using arrow-function gives intended behaviour. Why is it so?

Goal

Console must log a message only on clicking the button.


Set-up

const button_l = document.querySelector('.btn-left');

method-1 (Using arrow function as a callback)

button_l.addEventListener('click', () => {
    console.log('I am in')
});

method-2 (Using function as a callback)

button_l.addEventListener('click', changeSlide());

function changeSlide() {
    console.log('Am I globally present?');
}

Problem Facing

  1. Using method-1 gives the desired results.
  2. Using method-2, the function is called on page load, but when I remove the parentheses (from changeSlide() to changeSlide), it functions as it should, why is it so?

I think it is something very fundamentally obvious that I do not know.

I appreciate a link/reference to any helpful theory/understanding.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda