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

288
Visualizações
[INQUIRY ]JavaScript - Variable Initilization

function calculateArea(r){
  var area;
  if (r<=0){
    return 0;
  } else {
    area=Math.PI*r*r; 
    return area; 
  }
}

var radius = 5.2; 
var theArea=calculateArea(radius);
console.log("Area = " + theArea); 

New here, please let me know if I have posted incorrectly.

If the initialization of a variable is a function. Does the function execute automatically? For instance, see my code below. I get the desired output in the console log. Had I not included this function in the variable, would it have still ran?

   function calculateArea(r){
  var area;
  if (r<=0){
    return 0;
  } else {
    area=Math.PI*r*r; 
    return area; 
  }
}

var radius = 5.2; 
var theArea=calculateArea(radius);
console.log("Area = " + theArea); 

EDIT: Think I've got my head wrapped around this not. Thank you all!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

if you call it without any arguments

calculateArea()

r will be undefined, leading to Math.PI * r * r = number * undefined = NaN

and if you never used this

var theArea=calculateArea(radius);

then the function won't be executed automatically

about 4 years ago · Juan Pablo Isaza Relatório

0

No, defining a function doesn't run it automatically. (After all, if it requires an argument, like r in your case, what would that r be?)

You will need to call the function (calculateArea(1)) for it to be run, but you don't need to assign the return value to a variable (area = calculateArea(1)) for that to happen.

about 4 years ago · Juan Pablo Isaza Relatório
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