Furkan's Notes

Furkan's Notes

7 Object Methods That You Should Know

7 Object Methods That You Should Know

Listen to this article

1. Definition

In this tutorial we will take a look Object prototype and which methods does it provide. Let us get into it.

2. Objest.is()

Object.is() - JavaScript | MDN

In our first example we have and comprassion method which determines whether two values are the same value.

  • Returns: a boolean indicating whether or not the two arguments are the same value.
// Case 1: Evaluation result is the same as using ===
Object.is(25, 25);                // true
Object.is('foo', 'foo');          // true
Object.is('foo', 'bar');          // false
Object.is(null, null);            // true
Object.is(undefined, undefined);  // true
Object.is(window, window);        // true
Object.is([], []);                // false
var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo);              // true
Object.is(foo, bar);              // false

// Case 2: Signed zero
Object.is(0, -0);                 // false
Object.is(+0, -0);                // false
Object.is(-0, -0);                // true
Object.is(0n, -0n);               // true

// Case 3: NaN
Object.is(NaN, 0/0);              // true
Object.is(NaN, Number.NaN)        // true

3. Object.assign()

Object.assign() - JavaScript | MDN

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object

  • Returns: The target object.
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }

Properties in the target object are overwritten by properties in the sources if they have the same key. Later sources' properties overwrite earlier ones.

4. Object.entries()

Object.entries() - JavaScript | MDN

The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs.

  • Returns: An array of the given object's own enumerable string-keyed property [key, value] pairs.
  • The ordering of the properties is the same as that given by looping over the property values of the object manually.
const object1 = {name: "David", age: 23};

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

// "name: David"
// "age: 23"

5. Object.prototype.hasOwnProperty()

Object.prototype.hasOwnProperty() - JavaScript | MDN

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property

  • Returns: true if the object has the specified property as own property; false otherwise.
const object1 = {};
object1.property1 = 42;

console.log(object1.hasOwnProperty('property1'));
// expected output: true

console.log(object1.hasOwnProperty('toString'));
// expected output: false

console.log(object1.hasOwnProperty('hasOwnProperty'));
// expected output: false

6. Object.keys()

Object.keys() - JavaScript | MDN

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.

  • Returns:
const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]

7. Object.values()

developer.mozilla.org/en-US/docs/Web/JavaSc..

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.

  • Returns: An array containing the given object's own enumerable property values.
const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]

8. Object.prototype.toString()

developer.mozilla.org/en-US/docs/Web/JavaSc..

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

  • Returns: A string representing the object.
function Dog(name) {
  this.name = name;
}

const dog1 = new Dog('Gabby');

Dog.prototype.toString = function dogToString() {
  return `${this.name}`;
};

console.log(dog1.toString());
// expected output: "Gabby"

/* ---- */
const o = new Object();
o.toString(); // returns [object Object]

9. Optional Parameter for toString

For Numbers and BigInts toString() takes an optional parameter radix the value of radix must be minimum 2 and maximum 36.

By using radix you can also convert base 10 numbers (like 1,2,3,4,5,.........) to another base numbers, in example below we are converting base 10 number to a base 2 (binary) number.

let baseTenInt = 10;
console.log(baseTenInt.toString(2));
// Expected output is "1010"

let bigNum = BigInt(20);
console.log(bigNum.toString(2));
// Expected output is "10100"
 
Share this