
- 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
How to access the first value of an object using JavaScript?
In this article, you will understand how to access the first value of an object using JavaScript.
The first value of the object is the first property located at position[0] of the object. The object can be a key-value object or an array object.
Example 1
In this example, let's consider a key-value pair object.
const inputObject = {1: 'JavaScript', 2: 'Python', 3: 'HTML'}; console.log("A key-value pair object is defined and its values are: ", inputObject) console.log("
The first value of the object is: ") const firstValue = Object.values(inputObject)[0]; console.log(firstValue);
Explanation
Step 1 − Define a key-value pair object ‘inputObject’.
Step 2 − Define a variable ‘firstValue’ and assign the value of the first position of the object i.e (inputObject)[0] to it.
Step 3 − Display the value of ‘firstValue’ as result.
Example 2
const arrayObject = ['JavaScript', 'Python', 'HTML']; console.log("An array object is defined and its values are: ", arrayObject) console.log("
The first value of the object is: ") const firstValue = Object.values(arrayObject)[0]; console.log(firstValue);
Explanation
Step 1 − Define an array ‘arrayObject’.
Step 2 − Define a variable ‘firstValue’ and assign the value of the first position of the object i.e (arrayObject)[0] to it.
Step 3 − Display the value of ‘firstValue’ as result.
- Related Articles
- How to access an object value using variable key in JavaScript?
- How to access an object having spaces in the object’s key using JavaScript?
- How to access an object through another object in JavaScript?
- How to access cookies using document object in JavaScript?
- How to access a JavaScript object using its own prototype?
- How to get only first word of object's value – JavaScript?
- How to create an object and access its properties in JavaScript?
- How to add, access JavaScript object methods?
- How to add, access, delete, JavaScript object properties?
- JavaScript: How to Create an Object from Key-Value Pairs
- How to modify an array value of a JSON object in JavaScript?
- How to access and modify pixel value in an image using OpenCV Python?
- How to remove an object using filter() in JavaScript?
- What is an object in JavaScript and access any property ?**
- What is the 'new' keyword in JavaScript to create an object and how to access values?

Advertisements