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 trialJoe Nguyen
25,189 PointsDo I really have to do "var" instead of const?
On this challenge I have to do var and const can somebody tell me the difference between var and const I know they are the same thing but on another course I saw that it was better to use "const" instead of var
$(document).ready(function() {
const weatherAPI
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>What's the Weather Like?</title>
<script src="jquery.js"></script>
<script src="weather.js"></script>
</head>
<body>
<div id="main">
<h1>Current temperature: <span id="temperature"></span>°</h1>
</div>
</body>
</html>
3 Answers
Steven Parker
231,269 PointsThe ES2015 syntax introduced "const" and "let" for variable declarations, but this course is from 2014 and uses a validation engine that doesn't support the new syntax.
Also, it would only make sense to declare something "const" if you intialize it at the same time, since it cannot be changed later.
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 21,632 PointsIs there any difference between them?On how they work?
'const' is block-scoped and it can't be changed (though it is mutable so the values in objects and arrays assigned to 'const' can be changed). 'let' is also block-scoped, but it can be changed freely.
'var' is function-scoped and it can be changed or reassigned. It's the original though so it has the widest browser support, but it can get a little messy at times. A good rule of thumb for while you're getting started (if your project allows for ES2015) is to assign everything to 'const' at first and change it to 'let' when you need to reassign a value.
Here's a more in-depth breakdown: https://wesbos.com/javascript-scoping/
Joe Nguyen
25,189 PointsThank you Steven and Trent.
Joe Nguyen
25,189 PointsJoe Nguyen
25,189 PointsIs there any difference between them?On how they work?
Steven Parker
231,269 PointsSteven Parker
231,269 PointsVariables declared with "var" are hoisted, and have function scope. Those declared with "const" and "let" both have block scope and no hoisting. And as I mentioned before, anything declared "const" cannot be changed later in the program.