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 trial

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

$(document).ready(function() {
const weatherAPI
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>What's the Weather Like?</title>
  <script src="jquery.js"></script>
  <script src="weather.js"></script>
  <div id="main">
    <h1>Current temperature: <span id="temperature"></span>&deg;</h1>

3 Answers

Steven Parker
Steven Parker
229,644 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
229,644 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
.a{fill-rule:evenodd;}techdegree seal-36
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 21,632 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.