
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Get the first element of array in JavaScript
You can use simple for loop along with some if else condition to get the array’s first element in JavaScript.
The logic is that, first of all check the array length is greater than 1 or not if the length is not equal to 1 that means there is no element in the array. So, go to the else condition and set the value undefined and print any message at the console. If there is an element in an array, set the first index value to any variable and terminate the loop with the help of break and print the message at the console.
Example
var studentDetails= [ { "firstName":"John" }, { "firstName":"David" }, { "firstName":"Bob" }, { "firstName":"Mike" }, { "firstName":"Carol" } ] var firstObjectValue = ""; if(studentDetails.length > 1){ for(var index=0;index< studentDetails.length;index++){ firstObjectValue=studentDetails[index].firstName; break; } } else { firstObjectValue=undefined; } if(firstObjectValue!=undefined) console.log(firstObjectValue); else console.log("There is no element in the array");
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo184.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo184.js John
- Related Articles
- How to get the first element of an array in PHP?
- First element and last element in a JavaScript array?
- Finding the first redundant element in an array - JavaScript
- Detecting the first non-unique element in array in JavaScript
- Projection of arrays to get the first array element from MongoDB documents
- Finding first unique element in sorted array in JavaScript
- How to get the last element in JavaScript array?
- Finding the first unique element in a sorted array in JavaScript
- Constructing an array of addition/subtractions relative to first array element in JavaScript
- How to get only the first n% of an array in JavaScript?
- How to get the first n values of an array in JavaScript?
- Get the first element in an array and return using MongoDB Aggregate?
- Get the first and last item in an array using JavaScript?
- Get first element of each sublist in Python
- How to get First Element of the Tuple in C#?

Advertisements