What is the difference between let, const, and var?

I-Hub Talent is recognized as one of the best Full Stack Java training institutes in Hyderabad, offering a comprehensive program designed to equip learners with the skills needed to excel in today’s competitive IT industry. The course covers everything from Core Java, Advanced Java (Servlets, JSP) to Spring Boot, Hibernate, REST APIs, and databases like MySQL, along with essential front-end technologies such as HTML, CSS, JavaScript, and frameworks like React.

What sets IHub Talent apart is its practical, project-based learning approach. Students gain real-world exposure through live projects and industry-driven case studies, ensuring they can confidently apply their skills in professional environments. The training is led by experienced mentors who bring years of industry expertise, guiding students step-by-step from basics to advanced concepts.

In addition to technical training, IHub Talent provides career-focused support including resume building, mock interviews, and dedicated placement assistance, enabling learners to secure job opportunities with top companies. The curriculum is regularly updated to match the latest industry trends, ensuring students remain competitive.

Whether you are a fresher looking to start your career or a professional aiming to upgrade your skills, IHub Talent offers the perfect environment to master Full Stack Java development and launch a successful IT career.

1. var

  • Scope → Function-scoped (available within the function where it’s declared).

  • Hoisting → Hoisted and initialized as undefined.

  • Redeclaration → Can be re-declared in the same scope.

  • Reassignment → Allowed.

var x = 10; var x = 20; // ✅ redeclared x = 30; // ✅ reassigned

2. let

  • Scope → Block-scoped (limited to { } where it’s defined).

  • Hoisting → Hoisted but kept in Temporal Dead Zone (TDZ) until initialized (cannot use before declaration).

  • Redeclaration → ❌ Not allowed in the same scope.

  • Reassignment → ✅ Allowed.

let y = 10; // let y = 20; ❌ redeclaration not allowed y = 30; // ✅ reassignment allowed

3. const

  • Scope → Block-scoped (like let).

  • Hoisting → Hoisted but in TDZ.

  • Redeclaration → ❌ Not allowed.

  • Reassignment → ❌ Not allowed. (value is constant)

  • However, for objects and arrays, the reference is constant, but properties/elements can still be changed.

const z = 10; // z = 20; ❌ Error (cannot reassign) const obj = { name: "JS" }; obj.name = "JavaScript"; // ✅ allowed (modifying property)

Quick Comparison Table

Featurevarletconst
ScopeFunction-scopedBlock-scopedBlock-scoped
HoistingYes (initialized as undefined)Yes (TDZ)Yes (TDZ)
Redeclaration✅ Allowed❌ Not allowed❌ Not allowed
Reassignment✅ Allowed✅ Allowed❌ Not allowed
Use CaseLegacy codeVariables that changeFixed values / references

👉 Best practice in modern JavaScript:

  • Use const by default.

  • Use let if you need reassignment.

  • Avoid var unless working with old code.

Read More:


What is hoisting in JavaScript?

Visit Our IHUB Talent Training Institute in Hyderabad          

Comments

Popular posts from this blog

What is @Entity annotation?

Explain merge conflict and how to resolve it.

What is Spring Framework?