var stands for "variable" is used to declare variables that can be reassigned and are only available inside the function they're created in. They're function scoped.
var word = "hello" console.log(word) // returns "hello" word = "bye" // can be re-assigned ✅ console.log(word) // returns "bye"
const stands for "constant" and is used to declare variables that cannot be reassigned and are not accessible before they appear within the code. They're block scoped.
const word = "hello" console.log(word) // returns "hello" word = "bye" // cannot be re-assigned ❌ console.log(word) // throws an error as constants cannot be re-assigned
Variables declared using let can be reassigned but are similar to const i.e. block scoped. If variables are not created inside a function or block they are globally scoped.
A block is a set of opening and closing curly brackets.
Hope you learned something useful today! Peace Out ✌️
Top comments (0)