Difference between var and let in JavaScript

In JavaScript, we use both var and let to declare a variable. However, they are not understood the same. The differences between them are scope. var is function scoped while let is block-scoped.

If a variable is defined with let in a later code block, it returns an error of no existing variable, while it returns undefined (declared but no value) with var.

Example of var

console.log(book);
var book = 'Learn Flutter';
console.log(book);

Example of let

console.log(book);
let book = 'Learn Kotlin';
console.log(book);

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close