Avatar ·

How to correctly check if a variable is an array in JavaScript?

Hi everyone! I just started learning JavaScript and ran into a problem. I need to check if a variable is an array, but I'm not sure how to do it correctly. I know there's the `typeof` operator, but it doesn't work for arrays — it returns `"object"`. For example: ```javascript const arr = [1, 2, 3]; console.log(typeof arr); // "object" — useless! ``` I tried using `instanceof Array`, but I was told it's unreliable, especially if the code works with different frames or windows (iframes). I also saw someone checking with `Array.isArray()`, but I don't understand why this method is better. Also, I'm confused that in some old projects they use `Object.prototype.toString.call(arr) === '[object Array]'`. Is this legacy or a modern approach? Tell me