# Why the template literals not working?

**URL:** <https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [July 26, 2021, 8:11pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043 "2021-07-26T20:11:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![garry2090](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/garry2090/32/224414_2.png) [@garry2090](https://forum.freecodecamp.org/u/garry2090)\
**Post date:** [July 26, 2021, 8:11pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/1 "2021-07-26T20:11:08Z")

</div>

```
  **Your code so far**

```

```js

function Bird() {
let weight = 15;

this.getWeight = () => {return `${weight}`} //working
this.getWeight = function(){
  `${weight}`
  } //not working
}
//why ??

```

```
  **Your browser information:**

```

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36`

**Challenge:** Use Closure to Protect Properties Within an Object from Being Modified Externally

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/use-closure-to-protect-properties-within-an-object-from-being-modified-externally)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [July 26, 2021, 8:16pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/2 "2021-07-26T20:16:57Z")

</div>

It’s not a problem with template literals, your second function has no return statement.

You could simplify the first, by the way (because it’s a one-liner):

```auto
this.getWeight = () => `${weight}`

```

---

<div class="post-metadata">

**Author:** ![garry2090](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/garry2090/32/224414_2.png) [@garry2090](https://forum.freecodecamp.org/u/garry2090)\
**Post date:** [July 26, 2021, 8:20pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/3 "2021-07-26T20:20:16Z")

</div>

> [@jsdisco](#):
>
> `this.getWeight = () => `${weight}``

 ![Screenshot 2021-07-27 014946](https://global.discourse-cdn.com/freecodecamp/original/3X/5/3/53de43126778fce3077d7ac74ead6b845fdde5fa.png)

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [July 26, 2021, 8:29pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/4 "2021-07-26T20:29:25Z")

</div>

Right I see, I didn’t check the challenge. Both of your functions should actually fail.

It wants you to return the value of `weight`, which is a number. When you use a template literal, you’re converting the Number `15` into a String `'15'`, that’s why the tests fail.

---

<div class="post-metadata">

**Author:** ![garry2090](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/garry2090/32/224414_2.png) [@garry2090](https://forum.freecodecamp.org/u/garry2090)\
**Post date:** [July 26, 2021, 8:31pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/5 "2021-07-26T20:31:06Z")

</div>

Yep , Correct answer .

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [July 26, 2021, 8:37pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/6 "2021-07-26T20:37:08Z")

</div>

What would be the point of using a template literal here anyway if you don’t mind me asking?

---

<div class="post-metadata">

**Author:** ![garry2090](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/garry2090/32/224414_2.png) [@garry2090](https://forum.freecodecamp.org/u/garry2090)\
**Post date:** [July 26, 2021, 8:43pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/7 "2021-07-26T20:43:21Z")

</div>

I did it , just for fun.

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [July 26, 2021, 9:02pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/8 "2021-07-26T21:02:00Z")

</div>

Well, you did learn something, string literals are in fact (surprise, surprise) strings. As a rule of thumb, you should never change the data type unless you need to. If the return was used for some math you might end up with unexpected results (like string concatenation).

```auto
function Bird(weight) {
  let _weight = weight;
  this.getWeight = () => { return `${_weight}` }
}

let ducky = new Bird(15);
console.log(typeof ducky.getWeight()) // string
console.log(15 + ducky.getWeight()) // 1515

```

* * *

[The test](https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/use-closure-to-protect-properties-within-an-object-from-being-modified-externally.md) has a strict equals check so the type must match and the regex used won’t match on your code either. Experimenting is fine, just know a lot of challenges might break if you provide them with unexpected code.

---

<div class="post-metadata">

**Author:** ![garry2090](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/garry2090/32/224414_2.png) [@garry2090](https://forum.freecodecamp.org/u/garry2090)\
**Post date:** [July 26, 2021, 9:05pm UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/9 "2021-07-26T21:05:50Z")

</div>

I agree

> [@lasjorg](#):
>
> just know a lot of challenges might break if you provide them with unexpected code

Totally agree

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [January 25, 2022, 9:06am UTC](https://forum.freecodecamp.org/t/why-the-template-literals-not-working/471043/10 "2022-01-25T09:06:00Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
