Modelo

  • EN
    • English
    • Español
    • Français
    • Bahasa Indonesia
    • Italiano
    • 日本語
    • 한국어
    • Português
    • ภาษาไทย
    • Pусский
    • Tiếng Việt
    • 中文 (简体)
    • 中文 (繁體)

Adding a Property to an Object in JavaScript

Oct 14, 2024

In JavaScript, you can add a new property to an existing object using either dot notation or bracket notation. With dot notation, you simply specify the object name followed by a dot and then the new property name, like this: obj.newProperty = value;. With bracket notation, you use square brackets and a string to define the new property, like this: obj['newProperty'] = value; Both methods achieve the same result, but bracket notation is often used when the property name is dynamic or not a valid JavaScript identifier. By using these methods, you can easily add and update properties on objects in your JavaScript code.

Recommend