Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Object Properties in JavaScript

Object Properties in JavaScript

Object Properties in JavaScript


What Are Object Properties?

  • Properties are key-value pairs associated with JavaScript objects.

  • Each property has:

    • A key (also called name)

    • A value (can be any data type)

  • Properties define the characteristics or data of an object.


Defining Properties

const person = {  name: "Alice",  // key: name, value: "Alice"  age: 30,  isStudent: false};

Accessing Properties

Dot Notation

console.log(person.name);  // Alice

Bracket Notation

console.log(person["age"]); // 30
  • Useful when property names are dynamic or not valid identifiers.


Adding or Modifying Properties

person.city = "New York";           // Add new propertyperson["country"] = "USA";           // Another way to addperson.age = 31;                    // Modify existing property

Deleting Properties

delete person.isStudent;console.log(person.isStudent);  // undefined

Property Attributes (Advanced)

Properties have internal attributes:

AttributeDescription
valueThe property's value
writableCan the property's value be changed?
enumerableShows up in loops like for...in?
configurableCan property be deleted or changed?

You can check or define these using:

Object.getOwnPropertyDescriptor(person, "name");Object.defineProperty(person, "name", { writable: false });

Checking if Property Exists

"name" in person;           // trueperson.hasOwnProperty("age"); // true

Summary

OperationSyntax Example
Define propertyobj.key = value or obj["key"] = value
Access propertyobj.key or obj["key"]
Delete propertydelete obj.key
Check existence"key" in obj or obj.hasOwnProperty("key")

Want me to explain property descriptors or show how to iterate properties?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql