Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjoseph arias
9,269 PointsDefault value
I don't understand why = {} was added inside the parameter. Anyone know?
class Student {
constructor({ name, age, interestLevel = 5 } = {}) {
this.name = name;
this.age = age;
this.interestLevel = interestLevel;
}
}
2 Answers
Steven Parker
231,236 PointsThis is the syntax for a default value.
When you see an equal sign (=
) in an argument specification, it means that the argument will be given a default value if none is supplied in the call. In this case, the constructor function takes an object as the argument, and the default is an empty object, represented by the set of braces ({}
). So calling this constructor with no argument is the same as calling it with an empty object.
Then, the object parameter itself has a default of 5 for the interestLevel property. So calling the constructor with no argument, or calling it with an object that has no interestLevel property, will both cause the constructed class object to have an insterestLevel property set to 5.
Tom Geraghty
24,174 PointsThese tests might illustrate the differences:
class Student {
constructor({ name, age, interestLevel = 5 } = {}) {
this.name = name;
this.age = age;
this.interestLevel = interestLevel;
}
}
class StudentNoDefault {
constructor(name, age, interestLevel = 5) {
this.name = name;
this.age = age;
this.interestLevel = interestLevel;
}
}
Now some tests:
new Student()
// Student { name: undefined, age: undefined, interestLevel: 5 }
new Student({})
// Student { name: undefined, age: undefined, interestLevel: 5 }
new Student({},{})
// Student { name: undefined, age: undefined, interestLevel: 5 }
new Student({},{},{},{},{}) //add as many parameters as you want...
// Student { name: undefined, age: undefined, interestLevel: 5 }
new StudentNoDefault()
// StudentNoDefault { name: undefined, age: undefined, interestLevel: 5 }
new StudentNoDefault({})
// StudentNoDefault { name: {}, age: undefined, interestLevel: 5 }
new StudentNoDefault({},{})
// StudentNoDefault { name: {}, age: {}, interestLevel: 5 }
new StudentNoDefault({},{},{},{},{})
// StudentNoDefault { name: {}, age: {}, interestLevel: {} }
Seeing the pattern? In the above Student class, the parameters are defined as optional by using ={} which leaves any parameter not explicitly given to be equal to undefined. If you leave out the optional ={}, then the parameters (even empty Objects {}) are assigned to the class' property (in the order they are given as parameters).
Tests ran here: https://repl.it/languages/babel