Skip to content

Variables

There are 3 ways to declare variable in JS/TS.

Variable TypeBlock/Global ScopeCan be updatedCan be re-declare
varGlobalYY
letBlockYX
constBlockXX

You can check the simple explanation for each variable types.

js
var str = "var-a"

var str = "var-c" // Can be re-declare
str = "var-b" // Can be updated

console.log(str) // should be "var-b"
js
let str = "let-a"

let str = "let-c" // Error cannot be re-declare X
str = "let-b" // Can be updated

console.log(str) // should be "let-b"
js
const str = "const-a"

const str = "const-c" // Error cannot be re-declare
str = "const-b" // Error cannot be updated

console.log(str) // should be "const-a"