js-by-examples icon indicating copy to clipboard operation
js-by-examples copied to clipboard

Can I help update the declaration of variables?. A good practice!

Open jdavidleon opened this issue 1 year ago • 0 comments

Scope

let var const

function hi () { alert("hi") }

hi = function () { alert("hello") }

console.log(hi());  //"hello"

better

const hi = function () { alert("hi") }

hi = function () { alert("hello") }

console.log(hello);  // Uncaught TypeError: invalid assignment to const 'hi'
let obj = {};
obj = "string"; //"string"

better

const obj = {};
obj = "string";  // Uncaught TypeError: invalid assignment to const 'obj'

jdavidleon avatar Feb 16 '23 15:02 jdavidleon