Data Types

There are 5 simple data types in javascript : Undefined, Null, Boolean, Number, String. There is also a complex data type called object wich is an list of name-value pairs.

The Object Type

Objects are lists of name-value pairs with the following properties and methods :

  • constructor — The function that was used to create the object. In the previous example, the constructor is the Object() function.

  • hasOwnProperty(propertyName) — Indicates if the given property exists on the object instance (not on the prototype). The property name must be specified as a string (for example, o.hasOwnProperty(“name”)).

  • isPrototypeOf(object) — Determines if the object is a prototype of another object. (Prototypes are discussed in Chapter 5.)

  • propertyIsEnumerable(propertyName) — Indicates if the given property can be enumerated using the for-in statement (discussed later in this chapter). As with hasOwnProperty(), the property name must be a string.

  • toLocaleString() — Returns a string representation of the object that is appropriate for the locale of execution environment.

  • toString() — Returns a string representation of the object. valueOf() — Returns a string, number, or Boolean equivalent of the object. It often returns the same value as toString().

  • Object.keys() - method returns an array of a given object's own property names

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

console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]
  • Object.Assign() - method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

    const object1 = {
      a: 1,
      b: 2,
      c: 3
    };
    
    const object2 = Object.assign({c: 4, d: 5}, object1);
    
    console.log(object2.c, object2.d);
    // expected output: 3 5
    

results matching ""

    No results matching ""