3 method 😋 For older browsers and IE, you can use
const symbol = Symbol('symbol'); const array = [ 'string', 200, 0, undefined, null, symbol ]; 3 with other primitive types
3 also works great with other primitive types.
Caveats of const symbol = Symbol('symbol'); const array = [ 'string', 200, 0, undefined, null, symbol ]; 4
So far, I have shown you values where both
4 work interchangeably. However, there is one value, where they differ 🤭
Checking for Array of Objects using array.includes('string'); // true array.includes(200); // true array.includes(0); // true array.includes(undefined); // true array.includes(null); // true array.includes(symbol); // true 2
For a more versatile solution that works on other data types, you may want to use
This method is ideal for an array of objects.
In a previous code note, I talked about a quick & dirty way to check
Taking that concept, we can also use it to compare object element in an array like this:
4 are case sensitive:
To make it case insensitive, you could consider changing the case of the array like so:
But if you were using
3, you can do it in one line:
3 is really good for all modern browsers. However, if you need IE or older browser, you will need to use
Can I use?