0

consider pronoun as a scoped variable within an Object(e.g. let person = {}) Method(e.g. this.bio = function(){}) and this.gender as an Object property.

This piece of code works just fine: And returns, appropriate pronoun "he/she/they" depending on gender it contains

if(this.gender === "m" || this.gender === "male" || this.gender === "M" || this.gender === "Male") {
pronoun = "he";
} else if(this.gender === "f" || this.gender === "female" || this.gender === "F" || this.gender === "Female") {
pronoun = "she"
} else {
pronoun = "they"
}

Where as this piece of code works only returns "he"

if(this.gender === "m" || "male" || "M" || "Male") {
pronoun = "he";
} else if(this.gender === "f" || "female" || "F" || "Female") {
pronoun = "she"
} else {
pronoun = "they"
}

Can anyone help me understand why?? Thanks.

abappy
  • 5
  • 4
  • if you just open your console and write `if('') { console.log('hi') }` it does not console, because empty string is a falsy value so it's not going in the if statement. But any string inside of `if('a')` would console because it's true. So in your code, if this.gender is not equal to "m" and you say or "male" which always gonna return true inside of an if statement. That's why you get "he" :) – halilcakar Oct 18 '20 at 14:59
  • `if` will check `this.gender === "m"` first. And then it will check `"male"`. This is a [`truthy`](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) value. So, it will always enter the first block – adiga Oct 18 '20 at 15:07
  • 1
    Because `"m" || "male" || "M" || "Male" = "m"`, Your maybe after `["m","male","M","Male"].includes(this.gender)` – Keith Oct 18 '20 at 15:07
  • @HalilCakar okay, make sense. I thought js would consider this (expression) same as where it works(first piece of code (if) expression). Thank you for helping mw understand this distinction though, I appreciate it :) – abappy Oct 18 '20 at 15:11
  • @adiga thanks, make sense now, thanks to Halil's explanation!! I appreciate your time and effort :) – abappy Oct 18 '20 at 15:13
  • @Keith that's even better, spot on. Thanks Keith, you're awesome :) – abappy Oct 18 '20 at 15:17

0 Answers0