All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

For Key In Object JavaScript

Last Updated : Mar 11, 2024

For Key In Object JavaScript

In this article we will show you the solution of for key in object JavaScript, the object is a collection of properties, which can be reused multiple times.

In JavaScript, there is three types of objects- object keys, object values, and object entries. In this tutorial, we will see the object key property.

By using key in object we are able to get the key values in the object.

The syntax of the JavaScript object key is,

Object.keys(object)

Step By Step Guide On For Key In Object JavaScript :-

In the example below, we are going to understand the three different object properties. Let us see the code first.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> for key in object JavaScript </title>
    <style>
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
            font-family : 'Lucida Sans' , 'Lucida Sans Regular' , 'Lucida Grande' , 'Lucida Sans Unicode' , Geneva , Verdana , sans-serif ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> for key in object JavaScript </h3>
    <script>
        const object1 = {
            name : 'jack' ,
            gender : 'male' ,
            city : 'paris'
        } ;
        //key in object
        Object.keys(object1).forEach(( i ) => {
            console.log(object1[ i ]) ;
        } )
        //values in object
        let value = Object.values( object1 ) ;
        console.log( value ) ;
        //entries in object
        let entry = Object.entries( object1 ) ;
        console.log( entry ) ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. to add JavaScript Create a <script> tag.
  8. First create a constant variable with some object
  9. Here we used, Object.keys() used with a for loop to display the key and the values of the object
  10. Then use object.values() to display the values of the object in a form of an array.
  11. Last using object.entries() to display all the entries into an array.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know for key in object in JavaScript.

I hope this article on for key in object JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪