Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
[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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!