var, let and const - What, why and how - ES6 JavaScript Features

Fun Fun Function · 185K views · 2016 · 19:19

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it when telling people to stop using var and to understand how let and const scope differently, especially when debugging closure or global variable problems.

Recommended by 3 people across 3 different months, 2017 to 2018.

What it is

A 19-minute episode of Fun Fun Function in which Mattias Johansson explains the three JavaScript variable keywords, var, let and const. It covers scoping, hoisting and IIFEs, then closes with a short talk on why minimising mutable state is good practice.

What people said

Recommends Gabriel West · 2017-07 · Stack Overflow · on “Tictactoe game code”

I just copied and pasted OP code and didn't realize the `var`s at the top. You should definitely stop using `var`and instead opt for `let` or `const` for all your variables. `let` and `const` work slightly differently then `var`. `let` is block-scoped, while `var` is function scoped. `const` works like variables in other languages like java as in once a `const` is declared, you can't redeclare that variable to a new value. Heres a good video on the matter: [this video]

Recommends Jonathan Stellwag · 2018-07 · Stack Overflow · on “Angular 5 Member 'userLogin' not callable”

In JavaScript it's possible to set variables up to the most top level of your application. Watch this video if you don't know what i mean: [this video] This could maybe some reason for your trouble. To check out if you have some global variables set you need to debug your code as @callback mentioned and see what variables are available in your constructor, function,...

Recommends ykit9 · 2018-08 · Stack Overflow · on “undefined var in aggregate (mongoose)”

You are getting classical js closure error. It happens, because var is unsafe and "declared" outside of for loop. You can chek it yourself: for(var i = 0; i<10; i++){ console.log(i); // prints i from 0 to 9 } console.log(i); // prints 10 In context of async operations the actual value if i is taken from memory at the time, when those operation ends. When your query finish execution - the loop is ended, therefore the actual value of i is equal to -1 . To avoid this behaviour use es6 let instead of var . You can get more information in this videio [this video] .

Cites niedzwiedzwo · 2017-11 · Stack Overflow · on “Converting the timestamp into date object in Angular Typescript”

First fo all you shouldn't use var inside of a class declaration in TypeScript . You are only declaring which field can be populated, setting attributes on an instance. Second of all you shouldn't use var at all, use let or const instead [this video] . A more proper code should be: export class MyClassesComponent implements OnInit { date = '2017-11-27T09:10:00'; time = new Date(date); lesson : Lesson = {id: 10, name: "Test Lesson", image_url: "abc", start_time: time, end_time: time} constructor() { } ngOnInit() { } } See if that fixes your problem because your error says you implement OnInit [LINK] incorrectly ( onInit is a kind of pseudo-constructor), and it seems to be implemented ok. I think i