0% found this document useful (0 votes)
6 views

Difference Between Let, Var, and Const in ES6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Difference Between Let, Var, and Const in ES6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

let, var and

const - ES6
let
• 'let' allows you to declare variables that are
mutable, meaning their values can be
reassigned.

• It's perfect for situations where you need to


update or reassign a variable's value within its
scope.

• Used in loops and conditionals


Const
• 'const' declares variables that are immutable -
once assigned, their values cannot be changed.

• This provides a sense of security, ensuring that


critical values remain constant throughout your
code.

• Used when defining values like configuration


settings, mathematical constants, or references
to objects.
Var
• Variables declared with var are function- scoped
or globally scoped, but they are not block-scoped.

• Var variables can be re-declared and updated


within their scope.

• Since var doesn't have block scope, it can lead to


unintended issues when used in loops and
conditionals.
Aim for
Immutability
• While 'let' and 'const' offer distinct
advantages, embracing immutability whenever
possible can lead to more predictable and bug-
resistant code.

• Favor 'const' for values that shouldn't change


and leverage techniques like object.freeze) for
immutable objects.
FOLLOW

You might also like