Chisom Umeoke
2 min readJul 29, 2021

JAVASCRIPT OBJECTS

Object is a non-primitive data type in javascript. It is a collection of properties and this has to do with key and value. The property value can be a function and in this case, we refer to the properties as a method. Objects are variables too but they can accommodate lots of values.

At the end of this read, you will be able to understand the basics of javascript objects.

Javascript Object can be likened to an actual real life Object; let’s illustrate with a phone. A Phone has properties such as color, weight, model and so on. In the same manner does javascript objects have properties that define them.

An object can be initialized by using:

a. The object literal: which is a key:value pair in a curly bracket {}

let phone={color:’black’’,brand:’Iphone’,weight:’143g’,price:560,}; //Object Literal Syntax

b. The Object Constructor:

let phone = new Object(); //Object Constructor Syntaxphone.color=’black’;phone.brand=’Iphone’;phone.weight=’143g’;price= 560;

When a property is not assigned in an object, it returns as undefined.

Javascript Object Properties can take up multiple names but it has to be in quotes.

Example:

“chisom likes to eat”: true;

For In Loop

To find out the properties in an object, we use the for..in loop that is specially made for looping over objects, the syntax for this is :

for(let key in object).

Example:

let bio={sex:’boy’,age:25,status:’married’,}

If we wanted to find out all the keys in the “bio” object above.

We would do:

let bio={sex:’boy’,age:25,status:’married’,}for (let key in bio){console.log(key);} //sex, age, status

This will console log all the properties of bio, we can also use “props” too in place of “key”.

To get key array of an object we can use the Object.key()

let bio={sex:’boy’,age:25,status:’married’,}console.log(Object.keys(bio)) //(3) [“sex”, “age”, “status”]

While Object.key() is a recent way to do things, the major difference for Object() and for in is that; for in loops through inherited properties from their prototype. To avoid that, we use the hasOwnProperty() method to target a specific object

for (let key in bio) {if (bio.hasOwnProperty(key)) {console.log(`${key}`);}}

You can also look at other javascript data types like String, Numbers, Array, Boolean

Chisom Umeoke

Chisom Is a Tech Enthusiast. A Frontend Developer and a Design Thinker. She is drawn to user friendly products. She can be a fiction writer and she loves nature