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 trialGavin Lefleur
Full Stack JavaScript Techdegree Student 742 PointsHelp - TypeError for challenge 1 (Full stack Tech Degree - JS)
Hi Guys,
I am getting a 'Type error' using let command for one of the challenges in JS. Can anyone explain to me why?
let points = 100;
let bonusPts = 50;
let points += bonusPts;
console.log(points);
2 Answers
jb30
44,807 PointsYou can't redeclare values with let
in Javascript. let points += bonusPts;
should be points += bonusPts;
.
Gavin Lefleur
Full Stack JavaScript Techdegree Student 742 PointsHi jb30
I am still confused ... could you please break this down if you have a moment Regards
G
jb30
44,807 PointsThe line let points += bonusPts;
gives a syntax error. It is the short form of let points = points + bonusPts;
, which requires points
to already have a value on the right side and using it to compute the value for points
on the left side. Since let
is used to declare a variable for the first time, I would expect there to be a syntax error when you try to declare it using its own value, since if it already has a value, you have already declared the variable.
While all of my browsers agree that using let
with +=
is a syntax error, they disagree on whether let points = points + bonusPts;
is also a syntax error. If you don't know what browser your code will be run in, relying on redeclaration of your variables using let
could be an issue.
Gavin Lefleur
Full Stack JavaScript Techdegree Student 742 PointsGavin Lefleur
Full Stack JavaScript Techdegree Student 742 PointsThe original code and question is below:
Evaluate the code in app.js. The code currently produces a TypeError. Adjust the code so that the points variable holds the expected value.
const points = 100; const bonusPts = 50;
points += bonusPts; console.log(points);