What are Objects in JavaScript?

·

3 min read

image.png

What is Object in JavaScript?

In JavaScript object is a non-primitive data-type that allows you to store multiple collections of data.

Lets have one example

const person = { 
    name: 'hashnode',
    salary: 20
};
console.log(typeof person); // It is a object.

Writing code using Objects makes my code easier?

Yes, because Objects in programming can be a combination of variables, functions, and data structures. This means that objects can store values, you can use objects to manipulate values and combine them into more complex objects, like arrays ...

In generally we will be writing code to structure the output we want and then destructing the code which we written it makes easier only through objects in JavaScript.

How to Create a object?

Creation of object in JavaScript is very easy values can be given directly or in more technical manner it Creates a new object with the specified prototype object and properties.

let obj = { a: 1,  b: 2,  c: 3};
console.log(obj) // { a: 1,  b: 2,  c: 3}


const obj2 = Object.create(obj);
console.log(obj2); // {} it just creates a prototype of objects
obj2.a = 2;
obj2.b = 3;
obj2.c = 4;
console.log(obj2); // { a: 1,  b: 2,  c: 3}

What are different Objects in JavaScript?

In JavaScript values are objects except primitives. examples: Dates, Maths, Regular expressions, Arrays, Functions, Objects.

In JavaScript some primitives can be made as objects by using new key word. eg: Booleans, Numbers, Strings.

Strings : var v = new String(string);

What does Object.assign() do ?

The Object.assign() method only copies enumerable and own properties from a source object to a target object

const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }

What does Object.keys() do ?

The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.

const object1s = {
  a: 'String',
  b: 42,
  c: false
};
console.log(Object.keys(objects));  // a, b, c

What does Object.values() do ?

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop.

const object1s = {
  a: 'String',
  b: 42,
  c: true
};
console.log(Object.keys(objects));  // String, 42, true

What does Object.toString() do ?

The toString() method returns a string representing the object.

example:

 function emp(name, age, sal, ) {
  this.name = name;
  this.age = age;
  this.sal = sal;
}

const stremp = new emp('Gabby', '10', '4000');
console.log(stremp.toString());  //[Object Object] In order to get value in string need to 
                                                                                   write another function

const arr = [1, 2, 3];
console.log(arr.toString()); // "1,2,3"

String to array :   const arrs = (arr.toString()).split(",");
                             console.log(arrs); //[1, 2, 3]