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);
4 Respuestas
+ 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);
+ 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);
+ 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...