Open In App

How to access first value of an object using JavaScript ?

Last Updated : 12 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There are many methods to access the first value of an object in JavaScript some of them are discussed below: 

Example 1: This example accesses the first value object of GFG_object by using object.keys() method

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
  
<p id="GFG_UP" style="color:green;">
</p>
  
<button onclick="gfg_Fun()">
    find First
</button>
  
<p id="GFG_DOWN" style="color:green; font-size: 20px;">
</p>
  
<script>
    // Declare an object
    var GFG_object = {prop_1: "GFG_1",
            prop_2: "GFG_2", prop_3: "GFG_3"};
      
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
      
    // Use SON.stringify() function to take object
    // or array and create JSON string
    el_up.innerHTML = JSON.stringify(GFG_object);
      
    // Access the first value of an object
    function gfg_Fun() {
    el_down.innerHTML = GFG_object[Object.keys(GFG_object)[0]];
            }
</script>


Output:

How to access first value of an object using JavaScript ?

How to access first value of an object using JavaScript ?

Example 2: This example accesses the first value of object GFG_object by looping through the object and breaking the loop on accessing the first value. 

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
  
<p id="GFG_UP" style="color:green;"></p>
  
<button onclick="gfg_Fun()">
    find First
</button>
  
<p id="GFG_DOWN" style="color:green;font-size:20px;"></p>
  
<script>
    // Declare an object
    var GFG_object = {prop_1: "GFG_1",
            prop_2: "GFG_2", prop_3: "GFG_3"};
      
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
      
    // Use SON.stringify() function to take object
    // or array and create JSON string
    el_up.innerHTML = JSON.stringify(GFG_object);
      
    // Function to access the first value of an object
    function gfg_Fun() {
        for (var prop in GFG_object) {
            el_down.innerHTML = GFG_object[prop]
            break;
        }
    }        
</script>


Output:

How to access first value of an object using JavaScript ?

How to access first value of an object using JavaScript ?

Example 3: This example accesses the first value of objectGFG_object by using object.values() method. 

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
  
<p id="GFG_UP" style="color:green;"></p>
  
<button onclick="gfg_Fun()">
    find First
</button>
  
<p id="GFG_DOWN" style="color:green; font-size: 20px;"></p>
  
<script>
    // Declare an object
    var GFG_object = {prop_1: "GFG_value",
            prop_2: "GFG_2", prop_3: "GFG_3"};
      
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
      
    // Use SON.stringify() function to take object
    // or array and create JSON string
    el_up.innerHTML = JSON.stringify(GFG_object);
      
    // Function to access the first value of an object
    function gfg_Fun() {
        el_down.innerHTML = Object.values(GFG_object)[0];
            }        
</script>


Output:

How to access first value of an object using JavaScript ?

How to access first value of an object using JavaScript ?



Similar Reads

How to access an object having spaces in the object's key using JavaScript ?
Given an object and the task is to access the object in which the key contains spaces. There are a few methods to solve this problem which are discussed below: Approach 1: Create an object having space-separated keys.Use square bracket notation instead of dot notation to access the property. Example: This example implements the above approach. C/C+
2 min read
How Check if object value exists not add a new object to array using JavaScript ?
In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = { id: 1, name: ''Geeks1" }, { id: 2, name: '"Geeks2"
3 min read
How will you access the reference to same object within the object in PHP ?
In this article, we will see how we can access the reference to the same object within that object in PHP. To do that we have to use the "$this" keyword provided by PHP. PHP $this Keyword: It is used to refer to the current objectIt can only be used inside the internal methods of the class.$this keyword can refer to another object only if it is ins
2 min read
How to compare two objects to determine the first object contains equivalent property values to the second object in JavaScript ?
In this article, we are going to learn about comparing two objects to determine if the first object contains equivalent property values to the second object, In JavaScript, comparing the values of two objects involves checking if they have the same properties with corresponding values. Given two objects obj1 and obj2 and the task are to check that
5 min read
How to check if the provided value is an object created by the Object constructor in JavaScript ?
In this article, we will learn how to check if the provided value is an object created by the Object constructor in JavaScript. Almost all the values in JavaScript are objects except primitive values. There are several methods that can be used to check if the provided value is an object created by the Object constructor in JavaScript. Using the ins
3 min read
How to Append an Object as a Key Value in an Existing Object in JavaScript ?
In JavaScript, An object is a key-value pair structure. The key represents the property of the object and the value represents the associated value of the property. In JavaScript objects, we can also append a new object as a Key-value pair in an existing object in various ways which are as follows. Table of Content Using JavaScript Spread (...) Ope
2 min read
How to Add Duplicate Object Key with Different Value to Another Object in an Array in JavaScript ?
Adding duplicate object keys with different values to another object in an array in JavaScript refers to aggregating values under the same key from multiple objects in an array, creating a new object where each key corresponds to an array of associated values. Table of Content Using for...of LoopUsing reduce()Using for...of LoopFor...of loop in Jav
2 min read
Why we should prefer square operator over the dot operator to access the value from the object ?
The square notation should be used as much as possible as compared to the dot notation to access the value from the object in JavaScript. Let's first understand how to obtain the value by using the square notation and dot notation from the object. Square notation: In the square notation the keyName that is passed is treated as a variable and search
3 min read
How to select first object in object in AngularJS?
The main problem that we are dealing with is that for an object of objects reading the object of a particular index position is not as simple as a list. We cannot loop over it using ngFor as an object is not considered an iterable. The importance of this issue may arise when the data received from any source is an object containing objects(like JSO
3 min read
Access Object property case-insensitively in JavaScript
In this article, we will see how to access an object's properties without case-sensitivity, In this article, we are discussing how to access object property case-insensitively in JavaScript. Using toLowerCase() and Object.hasOwnProperty() MethodsUsing Object.keys() and toLowerCase() MethodsMethod 1: Using toLowerCase() and Object.hasOwnProperty() M
2 min read