Skip to content
the poor coder

what is property in javascript

Poor Man
Published
Reading time
1 min read
On this page

What is Property in JavaScript?#

In JavaScript, property refers to the characteristics or attributes of an object. It can be a value associated with the object or a function that can be executed on the object. Properties define the behavior of an object in JavaScript programming.

Properties are used to store and retrieve data from objects. They can be accessed using dot notation or square brackets notation. Dot notation is used when the property name is known and valid in JavaScript syntax, while square bracket notation is used when the property name is not known or contains invalid characters in JavaScript syntax.

Creating Properties#

Properties can be created using two methods:

  • Literal Notation: It involves creating an object and its properties at the same time. Here's an example:
JavaScript
const person = {
  name: 'John',
  age: 30,
  hobbies: ['reading', 'cooking']
}
  • Constructor Notation: It involves creating an object and then adding properties to it. Here's an example:
JavaScript
const person = new Object();
person.name = 'John';
person.age = 30;
person.hobbies = ['reading', 'cooking'];

Accessing Properties#

Properties can be accessed using dot notation or square bracket notation. Here's an example:

JavaScript
console.log(person.name); // Output: John
console.log(person['age']); // Output: 30

Deleting Properties#

Properties can be deleted using the delete operator. Here's an example:

JavaScript
delete person.hobbies;
console.log(person); // Output: {name: 'John', age: 30}

Overall, properties are a crucial aspect of object-oriented programming in JavaScript. They define the behavior of objects and allow us to manipulate and work with data in a structured way.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC