🌐
js owl
jsowl.com › get-the-first-element-of-an-array-in-javascript
How To Get The First Element Of An Array In JavaScript -With Examples - Js Owl
April 3, 2023 - This tutorial teaches you the different ... array in JavaScript and when it is appropriate to use them. ... You can use the array index 0 to get the first element of the array. Returns the first item if elements exist in the array. Returns undefined if the array is empty.
🌐
Designcise
designcise.com › web › tutorial › how-to-get-the-first-element-of-a-javascript-array
How to Get the First Element of a JavaScript Array? - Designcise
May 8, 2021 - When using array destructuring ... well as remove it from the original array, then you can simply use the Array.prototype.shift() method, for example, like so:...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › find
Array.prototype.find() - JavaScript - MDN Web Docs
3 weeks ago - The find() method of Array instances returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-array-exercise-3.php
JavaScript array: Get the first element of an array - w3resource
July 12, 2025 - // Function to get the first n elements of an array var first = function(array, n) { // Check if the input array is null, return undefined if true if (array == null) return void 0; // Check if the value of n is null, return the first element ...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript get first element of array
How to Get First Element of Array in JavaScript | Delft Stack
February 2, 2024 - The numeric key/index inside an array starts with 0. Using this index, you can retrieve the first element of an array in JavaScript. ... $index: It is a mandatory parameter that accepts only integer value that specifies the index of the element ...
🌐
xjavascript
xjavascript.com › blog › how-to-get-the-first-element-of-an-array
How to Get the First Element of an Array in JavaScript: Fixing the [object Object] Issue — xjavascript.com
The simplest and most widely used method is directly accessing the element at index 0 using square bracket notation: array[0]. If the array has elements, array[0] returns the first element. If the array is empty, it returns undefined.
🌐
TutorialsPoint
tutorialspoint.com › article › get-the-first-element-of-array-in-javascript
Get the first element of array in JavaScript
March 15, 2026 - In JavaScript, there are several ways to get the first element of an array. The most common and efficient approaches include using bracket notation, the at() method, or destructuring assignment.
Find elsewhere
🌐
xjavascript
xjavascript.com › blog › get-the-first-and-last-item-in-an-array-js
JavaScript: How to Get First and Last Array Items Without Undefined Errors – Fix Explained
The most common way to get the first item is arr[0]. But if the array is empty ([]), arr[0] returns undefined because there’s no element at index 0. const emptyArray = []; console.log(emptyArray[0]); // Output: undefined · For the last item, ...
🌐
Quora
quora.com › What-is-the-method-for-retrieving-the-first-element-of-an-array-in-JavaScript-without-using-a-for-loop
What is the method for retrieving the first element of an array in JavaScript without using a for loop? - Quora
Answer: You only need a for loop if you want to do more than one thing; picking the first element of an array only requires doing one thing. You can either pick element 0 of the array, simply using array[0] (replacing array with whatever the name of your array is), or you can use the at() method:...
🌐
Techie Delight
techiedelight.com › home › java › get first element of array in javascript
Get first element of array in JavaScript | Techie Delight
July 7, 2026 - Alternatively, with the Underscore JavaScript library, you can use the _.first method, which simply returns the first element of an array if no arguments are specified. Its aliases _.head and _.take can also be used.
Top answer
1 of 16
765

Conceptually, arrays in JavaScript contain array.length elements, starting with array[0] up until array[array.length - 1]. An array element with index i is defined to be part of the array if i is between 0 and array.length - 1 inclusive. If i is not in this range it's not in the array.

So by concept, arrays are linear, starting with zero and going to a maximum, without any mechanism for having "gaps" inside that range where no entries exist. To find out if a value exists at a given position index (where index is 0 or a positive integer), you literally just use

if (i >= 0 && i < array.length) {
  // it is in array
}

Now, under the hood, JavaScript engines almost certainly won't allocate array space linearly and contiguously like this, as it wouldn't make much sense in a dynamic language and it would be inefficient for certain code. They're probably hash tables or some hybrid mixture of strategies, and undefined ranges of the array probably aren't allocated their own memory. Nonetheless, JavaScript the language wants to present arrays of array.length n as having n members and they are named 0 to n - 1, and anything in this range is part of the array.

What you probably want, however, is to know if a value in an array is actually something defined - that is, it's not undefined. Maybe you even want to know if it's defined and not null. It's possible to add members to an array without ever setting their value: for example, if you add array values by increasing the array.length property, any new values will be undefined.

To determine if a given value is something meaningful, or has been defined. That is, not undefined, or null:

if (typeof array[index] !== 'undefined') {

or

if (typeof array[index] !== 'undefined' && array[index] !== null) {

Interestingly, because of JavaScript's comparison rules, my last example can be optimised down to this:

if (array[index] != null) {
  // The == and != operators consider null equal to only null or undefined
}  
2 of 16
353

Can't we just do this:

if(arrayName.length > 0){   
    //or **if(arrayName.length)**
    //this array is not empty 
}else{
   //this array is empty
}
🌐
Plain English
plainenglish.io › home › blog › javascript › get the first element of an array in javascript
Get the first Element of an Array in JavaScript
March 17, 2023 - Definitely, the most common way is to use zero index for access to the first element. Immutable concept, so the array is not changed. const animals = ['cat', 'dog', 'cow']; const first = animals[0]; console.log(first); // result: 'cat' Here, ...
🌐
JavaScript in Plain English
javascript.plainenglish.io › get-the-first-element-of-an-array-in-javascript-488a31cfc940
Get the first Element of an Array in JavaScript | by The Software Line | JavaScript in Plain English
September 8, 2024 - Definitely, the most common way is to use zero index for access to the first element. Immutable concept, so the array is not changed. const animals = ['cat', 'dog', 'cow']; const first = animals[0]; console.log(first); // result: 'cat' Here, ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › get-the-first-and-last-item-in-an-array-using-javascript
JavaScript - Get first and last Item of JS Array - GeeksforGeeks
August 17, 2026 - The Array.at() method in JavaScript allows to access elements of an array by their index. To get the first and last elements of an array using the at() method, pass the desired indices to the method. The first element has an index of 0.
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-the-first-non-null-undefined-argument-in-javascript
How to get the first non-null/undefined argument in JavaScript?
One method that can be used to get the first non-null/undefined argument in JavaScript is the Array.prototype.find() method. This method returns the value of the first element in an array that passes a given test. In our case, we can use this method to find the first non-null/undefined argument ...
🌐
FlashPunk Developers
developers.useflashpunk.net › t › how-can-i-find-the-first-empty-element-in-an-array › 673
How can I find the first empty element in an array? - help - FlashPunk Developers
October 17, 2013 - Hey guys! I don’t do much work with arrays and am still fairy new to FlashPunk and its special methods. Say I have an array, it is my inventory, it is currently filled with 9 empty inventory images. How would I go about searching the array for the first empty slot and then replacing that ...
🌐
GeeksforGeeks
geeksforgeeks.org › get-the-first-and-last-item-in-an-array-using-javascript
JavaScript – Get first and last Item of JS Array | GeeksforGeeks
November 25, 2024 - The Array.at() method in JavaScript allows to access elements of an array by their index. To get the first and last elements of an array using the at() method, pass the desired indices to the method. The first element has an index of 0.
Top answer
1 of 6
4

You are pushing an element in array at 1st index. So, javascript by default put undefined at 0th index.

So, at the end of each your array will be

[undefined, 2];

This is the reason you get the length as 2.

You can use push to add element in array:

$.each([1, 2, 3], function (index, element) {
    if (element == 2) {
        arr.push(element);
        elem++;
    }
});

Demo: https://jsfiddle.net/tusharj/moay7y95/2/

The push() method adds one or more elements to the end of an array and returns the new length of the array.

Docs: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push

2 of 6
3

Best solution: Use .filter

var arr = [1, 2, 3].filter(function(value){
    return value == 2
});

If the return value is true, the element is included in the returned array, otherwise it is ignored. This is pure js, so you don't need jquery. See documentation about .filter

Your problem: Use .push

Try using the .push method on the array (see Mozilla's documentation).

var arr = new Array();
$.each([1,2,3], function(index,element){
    if (element == 2){
        arr.push(element);
    }
});

The .push method will dynamically grow your array as elements are added.

Your problem is that the value of index inside your function is a reference point in your initial array. This means that if you look at the returned array, you will find [undefined, 2] and so the length is returning as 2. If your condition were element == 3 you would have two empty slots.

Alternatively: Use .grep

Another jQuery method is $.grep. See http://api.jquery.com/jquery.grep/

var arr = $.grep([1, 2, 3], function (value) {
    return value == 2
});

This is jQuery's implementation of javascript's .filter.