Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AJAX Basics (retiring) AJAX and APIs Make a JSONP request

Do 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

weather.js
$(document).ready(function() {
const weatherAPI
});
index.html
<!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>&deg;</h1>
  </div>
</body>
</html>

3 Answers

Steven Parker
Steven Parker
218,635 Points

The 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.

Is there any difference between them?On how they work?

Steven Parker
Steven Parker
218,635 Points

Variables 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.

Trent Stenoien
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 20,034 Points

Is 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/

Thank you Steven and Trent.