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

128
Views
Why does this happen when I console.log a function in javascript?

when I console.log this function, why does it print 12 and undefined? Shouldn't it just be undefined as the function is not returning anything?

function area(w,h) {
console.log(w*h)
}

console.log(area (3,4))

EDIT: Sorry super new and I thought that regardless of what is printed by the console.log, the second console.log would receive only what the function returned.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

function area(w,h) {
console.log(w*h) // Runs first and logs 12
}

console.log(area (3,4)) // Log undefined as the function doesn't return anything
about 4 years ago · Juan Pablo Isaza Report

0

The area function is logging the area, which is where 12 comes from.

The second undefined is the output of the console.log itself.

Generally, when you run anything which does not have output in a REPL, JavaScript adds an extra undefined

about 4 years ago · Juan Pablo Isaza Report

0

Because you already print it once

function area(w,h) {
  console.log(w*h)
}

area (3,4);

or

function area(w,h) {
  return (w*h)
}

console.log(area (3,4));

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!