DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Majid Tech
Majid Tech

Posted on

Javascript Object basics

What is an object in JavaScript?

Any thing that is not a js primitive is an object. An object is a collection of keys with there own values or key value pairs. Each value can be of any type.

  • An object is stored in the heap memory. All javascript objects maintain a reference to it, as opposed to a full copy of it. When checking for object equality, it checks the reference - not the actual value of properties making objects mutable.

How do you create and object?

Here are three ways we create an object in javaScript are:

  1. Object literal

    // we are literaly typing out our object. 
    const objectOne = {};
                      πŸ‘†πŸ½
    
  2. new Object();

    // use the js new key word to create a new object.
    const objectTwo = new Object();
                          πŸ‘†πŸ½
    
  3. Object.create({ })

    // creates a new object from an existing object.
    const objectTwo = Object.create({ });
                                πŸ‘†πŸ½
    

    Properties

    • Property is another term used for key value pairs that are stored in an object.
    • Names of properties are unique values that can be coerced to a string not including words like function, var, return. The name or key points to a value.
    • Property values can be of any type. Including functions witch we call methods.

    Manipulating Properties

    Setting properties can be done at two stages.

    • During the creation of our object.
    • After the creation of our object.

    Adding properties during its creation

    • as of ES6 (ECMAScript 6) we can also use a new short hand for setting properties whos value and name are the same.
    // in the creation on this object we are declaring its properties.
     const anime = { 
        title: 'naruto',
        discription: 'an anime about a ninja who is just trying to be hokage',
      isLigit, 'isLigit'
        }
    
        const anime = {
        title: 'naruto',
        discription: 'an anime about a ninja who is just trying to be hokage',
      isLigit // ES6 no need to state the value if it is the same as the key
        }
    

    Two ways of adding properties after its creation

    • Dot notation & Bracket notation
      Dot notation can only access names or keys that do not stat with a digit or does not include spaces

      // this works
      anime.rating = '100/10';
      // this does not 
      anime.my name = 'Jay';
      

      Bracket notation has no problem accessing names or keys that starts with a digit or contains spaces.

      anime[20] = '100/10'
      anime['my name'] = 'Jay';
      

      Getting properties

      We can properties in the same way we set them.

      const anime = anime.title;
      console.log(anime) // logs 'naruto'
      
      const anime = anime['title'];
      console.log(anime) // logs 'naruto'
      

      Deleting properties.

      Object Properties can be deleted using the delete keyword.

      delete anime.title;
      

Top comments (0)

🌚 Life is too short to browse without dark mode