why does this function work like this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why does this function work like this?

why does this function work like this? function compareMembers(person1, person2 = person){ if (person1 !== person2){ console.log("Not the same!") } else { console.log("They are the same!") } } const person = { name: 'Adel' } compareMembers(person);

1st Oct 2022, 8:17 PM
🕸carpe diem🕸
🕸carpe diem🕸 - avatar
4 Answers
+ 4
I think because you only have the one person to compare... Change the last line of your code to: const person1 = "Adelina"; const person2 = "Adeline"; compareMembers(person1, person2);
1st Oct 2022, 8:45 PM
Ausgrindtube
Ausgrindtube - avatar
+ 4
function compareMembers(person1, person2 = person.name){ if (person1 !== person2){ console.log("Not the same!") } else { console.log("They are the same!") } } const person = { name: 'Adelina' } compareMembers(person.name); compareMembers('Adel'); console.log(person.name);
1st Oct 2022, 8:52 PM
SoloProg
SoloProg - avatar
+ 4
What is your expectation by there? You are comparing same object by passing "person" to person1 with person object defaultly set to person2. Both are points to same same reference object. They will be equal so it prints else part...
1st Oct 2022, 9:19 PM
Jayakrishna 🇮🇳
1st Oct 2022, 8:18 PM
🕸carpe diem🕸
🕸carpe diem🕸 - avatar