JavaScript Object Literal

dheeraj korangadheeraj koranga
3 min read

In JavaScript, an object literal is a simple way to define an object using a set of key-value pairs. You define an object literal by using curly braces {} and specifying properties inside it.

const student ={
    Name : "dheeraj",
    Uid : 9856,
    Dep : "cse",
    Course : "BE",
    Subj :['python','java','ai/ml']
};

Accessing Object Properties (Two Ways)

You can access the values of object properties in two ways:

  1. Dot notation: objectName.propertyName

     console.log(student.name);   // dheeraj
     console.log(student.uid);    // 9856
    
  2. Bracket notation: objectName["propertyName"]

     console.log(student["Name"]);  // dheeraj
     console.log(student["Uid"]);   // 9856
    

Automatic Conversion of Keys to Strings

JavaScript automatically converts keys to strings in an object. You can use null, undefined, numbers, or other types as keys, but they will all be converted to strings internally.

let obj = {
    null: "This is null",
    undefined: "This is undefined",
    123: "This is a number"
};
console.log(obj["null"]);         // Output: "This is null"
console.log(obj[undefined]);      // Output: "This is undefined"
console.log(obj[123]);            // output : This is a number

Adding, Updating, and Deleting Properties in Object Literal

  • Adding or Updating a Property: You can add or update properties using dot notation or bracket notation.

  • Deleting a property: You can delete a property using the delete operator.

const student ={
    name : "dheeraj",
    uid : 9856,
    dep : "cse",
    course : "BE",
    subj :['python','java','ai/ml']
};

student.course="B tech";
student.gender="M";

Objects of Objects

Objects can contain other objects as properties, creating nested objects.

const classInfo = {
    dheeraj: {
        grade : "A+",
        city : "pune"
    },
    david:{
        grade : "f",
        city : "london"
    }
};

console.log(classInfo.dheeraj.city);   // pune

Array of Objects

An array of objects is an array where each element is an object. This is useful for managing collections of related objects.

let employees = [
    { name: "Alice", age: 28 },
    { name: "Bob", age: 35 },
    { name: "Charlie", age: 32 }
];

console.log(employees[0].name);  // Output: "Alice"

JavaScript Object vs Object Literal

  • JavaScript Object: It is a general term for any object in JavaScript. Objects can be created using the Object() constructor, or an object literal.

  • Object Literal: It is a specific way to create an object using {} with key-value pairs directly written inside.

  • Using Object() constructor:

let person = new Object();
person.name = "John";
person.age = 30;
  • Using an object literal:
let person = {
    name: "John",
    age: 30
};

Key Difference*: The object literal is a simpler, more concise way to create objects compared to using the Object() constructor.*

Math Object in JavaScript

The Math object in JavaScript provides built-in methods for mathematical operations. You don't need to instantiate the Math object, as it's a global object.

Common Math Methods:

  1. Math.abs(): Returns the absolute value of a number.

  2. Math.pow(): Returns the base raised to the power of the exponent.

  3. Math.floor(): Rounds a number down to the nearest integer.

  4. Math.ceil(): Rounds a number up to the nearest integer.

  5. Math.random(): Returns a random number between 0 (inclusive) and 1 (exclusive).

**********************************************************************

PRACTICE QUESTION

Qs1. Create a program that generates a random number representing a dice roll.
[The number should be between 1 and 6].

let dice_roll = Math.random()*6;
Math.ceil(dice_roll);

Qs2. Create an object representing a car that stores the following properties for the car: name, model, color.
Print the car’s name.

const car={ 
    name: "tata Harrier",
    model: "top-782 M",
    color: "Black"
}; 
console.log(car.name);

Qs3. Create an object Person with their name, age and city.
Edit their city’s original value to change it to “New York”.
Add a new property country and set it to the United States

**********************************************************************

0
Subscribe to my newsletter

Read articles from dheeraj koranga directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

dheeraj koranga
dheeraj koranga