# freeCodeCamp Challenge Guide: Use class Syntax to Define a Constructor Function

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-class-syntax-to-define-a-constructor-function/301212>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 5:50am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-class-syntax-to-define-a-constructor-function/301212 "2019-08-03T05:50:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 5:50am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-class-syntax-to-define-a-constructor-function/301212/1 "2019-08-03T05:50:28Z")

</div>

# [Use class Syntax to Define a Constructor Function](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function)

* * *

## Problem Explanation

In this lesson, you are defining the Vegetable object using class syntax.

* * *

## Hints

### Hint 1

Create the class called `Vegetable`. It will contain the necessary details about the `Vegetable` object.

### Hint 2

Put a constructor with a parameter called `name`, and set it to `this.name`.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> class Vegetable {
> constructor(name) {
> this.name = name;
> }
> }
> 
> const carrot = new Vegetable("carrot");
> console.log(carrot.name); // => should be 'carrot'
> 
> ```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:59pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-class-syntax-to-define-a-constructor-function/301212/2 "2025-01-08T15:59:04Z")

</div>

Closed on January 8, 2025, 3:59pm UTC


