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

321
Views
Can someone explain why this code print "Wild" in javascipt?

Hi I have a question about this code, why does it prints "Wild" instead of "Tabby"

var cat = { name: "Athena" };

function swap(feline) {
    feline.name = "Wild";
    feline = { name: "Tabby" };
}
swap(cat);
console.log(cat.name);

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

0

feline as an identifier is a local variable for your function. At the beginning it refers to cat from outside, and thus changing a part of the object is visible outside.

However when you set feline to a brand new object, that happens locally.

var cat = { name: "Athena" };

function swap(feline) {
    console.log("feline, cat:",feline.name,cat.name,"feline===cat:",feline===cat);
    feline.name = "Wild";
    console.log("feline, cat:",feline.name,cat.name,"feline===cat:",feline===cat);
    feline = { name: "Tabby" };
    console.log("feline, cat:",feline.name,cat.name,"feline===cat:",feline===cat);
}
swap(cat);
console.log("cat:",cat.name);

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!