Javascript — Const v.s Let

  • Const : block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment, and it can't be redeclared.
  • Let: statement declares a block-scoped local variable, optionally initializing it to a value.
  • Reassignment: You can only reassign value with the variable declared by let. If you try to reassign a value declared by const, you will get below TypeError
  • Redeclared: As both const and let are block scope varible, you awill get syntx error if you are trying to redeclare it in the same block({})
  • Block-scoped: Follow up on the previous point, if you try to declare it in the global level first and redeclare it in block level, you will not get any error.
  • Update call by reference property: This is one of the feature that people get confused easily. The definition of reassignment we are referring here is actually reassignment on reference. .Because both array and Object are calll be reference. This explain why you are able to push new value to an array that been declared with const and change the property of object that declare with const
  • use Const when you declare a variable that doesn’t want anyone else to change it
  • const functionName = () =>{}
  • use let in for loop

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Toddypet

Toddypet

Hello, I am Heidi. I started this medium blog to post all of the note I have when I am learning new things. Hope you find it useful as well:)