Properties: data attached to an object
An empty Dog class produces objects that are all identical, so there is nothing to tell one dog from another. To make them differ you attach data to each object. In JavaScript that data is called a property. Other languages call the same idea a field or an attribute, which is why this lesson is called "class fields".
Setting properties
JavaScript objects are dynamic: you don't have to declare a property before using it. Assigning to it with the dot operator creates it:
class Dog {
}
let dog = new Dog();
dog.name = "Rex";
dog.age = 4;
After these two assignments, dog has a name property equal to "Rex" and an age property equal to 4. The class itself stayed empty. The properties belong to the one object, not to the class.
Reading properties
You read a property with the same dot operator:
console.log(dog.name); // Rex
console.log(dog.age); // 4
Reading a property that was never set doesn't throw an error. It gives undefined:
let other = new Dog();
console.log(other.name); // undefined
This is the source of the most common property bug, covered below.
Each object has its own properties
Two objects made from the same class keep separate properties:
let rex = new Dog();
let luna = new Dog();
rex.name = "Rex";
luna.name = "Luna";
console.log(rex.name); // Rex
console.log(luna.name); // Luna
Setting rex.name never touches luna.name. Because each object has its own data, a program can hold many dogs at once.
Changing and storing values
A property is not fixed once set. Assigning again replaces the old value, so dog.age = 5 turns a four-year-old dog into a five-year-old one. A property can also hold any kind of value: a number, a string, a boolean, an array, or even another object, as in dog.tricks = ["sit", "roll"].
Other ways to work with properties
- Square brackets.
dog["name"]is the same asdog.name. Use brackets when the property name is stored in a variable. - Checking existence.
"name" in dogistrueonce the property has been set, andObject.keys(dog)returns the list of property names:["name", "age"]. - Removing.
delete dog.ageremoves the property, anddog.ageisundefinedafterwards. - Field declarations. Modern JavaScript also lets you declare fields with default values in the class body, as in
class Dog { name = "unknown"; age = 0; }. Every newDogstarts with those values. Setting properties by assignment, as in this lesson, works with or without such a declaration.
The exercise, step by step
The starter code creates the dog and prints a sentence, but the two lines in between are empty:
let dog = new Dog();
console.log(dog.name + " is " + dog.age + " years old");
Without them, dog.name and dog.age are both undefined, and the output is undefined is undefined years old. The only step is to fill the gap: set dog.name to "Rex" and dog.age to 4. Strings need quotes and numbers don't.
The solution in JavaScript
class Dog {
}
let dog = new Dog();
dog.name = "Rex";
dog.age = 4;
console.log(dog.name + " is " + dog.age + " years old");
The output is:
Rex is 4 years old
Common mistakes
- Typos create new properties. Because assignment creates a property when it is missing,
dog.nmae = "Rex"raises no error. It adds a property callednmae, anddog.namestaysundefined. When output showsundefined, check the spelling of the property on both the writing side and the reading side. - Setting properties on the class instead of the object.
Dog.name = "Rex"does not give any dog a name. Properties are set on the object created withnew, heredog. - Setting the property before the object exists.
dog.name = "Rex"placed abovelet dog = new Dog();fails with aReferenceError, because the variable can't be used before its declaration. - Forgetting quotes around text.
dog.name = Rexlooks for a variable calledRexand fails withReferenceError: Rex is not defined. - Repeating assignments for every object. Setting the same properties line by line after each
newis easy to get wrong. The next lesson shows constructors, which set them as the object is created.
Key takeaways
- A property is data stored on one object, written
object.property. - Assigning to a property that doesn't exist creates it; reading one that doesn't exist gives
undefined. - Each object has its own properties, even when they come from the same class.
- A misspelled property name silently creates a new property, so
undefinedin the output usually points to a typo.