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