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

148
Views
how does function has access to const before declaration?

I recently saw this questions:

function compareMembers(person1, person2 = person) {
  if (person1 !== person2) {
    console.log('Not the same!');
  } else {
    console.log('They are the same!');
  }
}

const person = { name: 'Lydia' };

compareMembers(person);

By seeing this, I am wondering how person can be the default value for person2.

const are not hoisted unlike var. And even if I try to do a console.log(person)

before function compareMembers I am getting Reference error.

IF any one can explain me how come the function has access to person as default value it would be really a great help.

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

0

Your code will run like this:

  • Stored variables to Stack (compareMembers, person). And referent to Heap Memory
  • Stored object, function to Heap Memory (dynamic)
{ name: 'Lydia' }

and

function compareMembers(person1, person2 = person) {
  if (person1 !== person2) {
    console.log('Not the same!');
  } else {
    console.log('They are the same!');
  }
}

Then run Call Stack

  1. compareMembers(person)

You can read document of call stack and stack and heap memory

about 4 years ago · Juan Pablo Isaza Report

0

JavaScript, as you know, is applied line by line In the first part, you came and created the function and did what you wanted inside

You take the first input from the user and the second input by default, if it does not work, you fill it with a variable

Now, in the next part, you created a variable, my friend, and you gave a function to this input, and this actually applies to both of your inputs, and this is absolutely true.

It only works in the opposite condition in your function if you give two inputs with different values

I hope I was able to help my friend

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!