For Key In Object JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Anjali
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>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- to add JavaScript Create a <script> tag.
- First create a constant variable with some object
- Here we used, Object.keys() used with a for loop to display the key and the values of the object
- Then use object.values() to display the values of the object in a form of an array.
- 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.