-1

I have an array and a name

var name = "Karen"

var arr = [ 
"Karen",
"Ronald",
"Mcdonald"
];

// Code here

I want to check if name is indexed in arr.
If it is then console.log(true) but if not I only want to console.log(false); one time.
I'm logging false every time the name doesn't match that is my problem.

Parking Master
  • 551
  • 1
  • 4
  • 20
AmandaConda
  • 223
  • 3
  • 13
  • 1
    Please add the code you've attempted to your question as a [mcve]. – Andy Nov 04 '21 at 01:00
  • 2
    Sounds like you want `console.log(arr.includes(name))`. That will log only once; `true` if `name` is in the array, `false` otherwise. See [Array.prototype.includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) – Phil Nov 04 '21 at 01:03

1 Answers1

0

It seems simple enough.

const name = "Karen"

var arr = [
"Karen",
"Ronald",
"Mcdonald"
];
if (arr.indexOf(name) > -1) {
  console.log(true);
} else {
  console.log(false);
}

Or Array.prototype.some:

var name = "Karen";

var arr = [
"Karen",
"Ronald",
"Mcdonald"
];
console.log(arr.some(n => n == name))

MDN Reference

Phil
  • 157,677
  • 23
  • 242
  • 245
Parking Master
  • 551
  • 1
  • 4
  • 20
  • 1
    `? true : false` is redundant. Also, why suggest `some` and `indexOf`, but not `includes`? – Sebastian Simon Nov 04 '21 at 01:07
  • `includes` will return if the array includes anything (e.g., `["Value"].includes("a"); // true`; as with `["Value"].indexOf("a") > -1; // false`;) – Parking Master Nov 04 '21 at 01:26
  • 1
    `[ "Value" ].includes("a")` is `false`. – Sebastian Simon Nov 04 '21 at 01:28
  • 1
    @ParkingMaster you seem to be confusing `String.prototype.includes()` and `Array.prototype.includes()`. – Phil Nov 04 '21 at 01:28
  • @Phil so that means then you should use indexOf – Parking Master Nov 04 '21 at 01:30
  • 1
    For this, no. I'd use `arr.includes(name)`. I also wouldn't use a redundant ternary – Phil Nov 04 '21 at 01:32
  • @ParkingMaster It’s advisable to follow the documentation again: [`Array.prototype.includes`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/includes), [`String.prototype.includes`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String/includes). Don’t accidentally coerce an array to a string, if that’s what you had in mind. – Sebastian Simon Nov 04 '21 at 01:32
  • @Phil I use `true : false` because if it returns something like `null` or `undefined` it'll be logged as `false`. – Parking Master Nov 04 '21 at 01:34
  • 1
    But [`Array.prototype.some()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#return_value) **only** returns a Boolean – Phil Nov 04 '21 at 01:35
  • @ParkingMaster Again, the documentation is right here: [`Array.prototype.some`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/some#return_value). _“Return value: `true` if the callback function returns a truthy value for at least one element in the array. Otherwise, `false`.”_. Note how `null` or `undefined` aren’t options. If in doubt, the [specification](//tc39.es/ecma262/#sec-array.prototype.some) is more detailed. Theoretically, `some` can throw a TypeError, but its only possible return value is a boolean. – Sebastian Simon Nov 04 '21 at 01:37