Monday, 4 Aug 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML

    What are the different types of HTML tags?

    By Chief Editor

    What are the async and defer attributes in the “script” tag?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor
  • JavaScript

    What is the Event Loop in JavaScript?

    By Chief Editor

    What is Arrow and Normal Function in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor

    What is a Promise in JavaScript, and what are its parameters?

    By Chief Editor
  • Frontend Interview

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Backend Interview

    What is Symentic HTML?

    By Chief Editor

    What is props drilling in React?

    By Chief Editor

    What is memoization in JavaScript?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is one-way data binding in React?

    By Chief Editor

    Explain the uesReducer and useContext hooks in React

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.
Home Blog Explain var let and const in JavaScript with Example.
JavaScript

Explain var let and const in JavaScript with Example.

Chief Editor
Last updated: February 16, 2025 5:36 pm
Chief Editor
Share
SHARE
In JavaScript, var, let, and const are used to declare variables. They differ in scope, reassignability, and hoisting behavior. Let’s break it down:

1. var

  • Introduced: Before ES6 (ES5 and earlier)
  • Scope: Function-scoped (available throughout the entire function)
  • Hoisting: Hoisted to the top with undefined value
  • Reassignable: Yes
  • Redeclarable: Yes

Example:

javascriptCopyEditvar x = 10;
function example() {
    var y = 20;
    console.log(y); // 20
}
console.log(x); // 10
// console.log(y); // Error: y is not defined (function scoped)

2. let

  • Introduced: In ES6 (2015)
  • Scope: Block-scoped (available only within {})
  • Hoisting: Hoisted but not initialized (temporal dead zone)
  • Reassignable: Yes
  • Redeclarable: No (within the same scope)

Example:

javascriptCopyEditlet a = 30;
if (true) {
    let b = 40;
    console.log(b); // 40
}
console.log(a); // 30
// console.log(b); // Error: b is not defined (block scoped)

3. const

  • Introduced: In ES6 (2015)
  • Scope: Block-scoped
  • Hoisting: Hoisted but not initialized (temporal dead zone)
  • Reassignable: No
  • Redeclarable: No

Example:

javascriptCopyEditconst PI = 3.14;
if (true) {
    const GRAVITY = 9.8;
    console.log(GRAVITY); // 9.8
}
console.log(PI); // 3.14
// PI = 3.1416; // Error: Assignment to constant variable

Summary Table:

KeywordScopeHoistingReassignableRedeclarable
varFunction ScopeYesYesYes
letBlock ScopeYesYesNo
constBlock ScopeYesNoNo

Key Takeaways:

Avoid var unless working with legacy code.

Prefer let for variables that can change.

Use const for constants or values that don’t change.

Share This Article
Email Copy Link Print
Previous Article What is Position in CSS?
Next Article What is hoisting in JavaScript with an example?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

What is Block level Element and Inline Level Element?

Block-level Elements Block-level elements occupy the entire width of their parent container (by default) and…

By Chief Editor

What is Doctype HTML in HTML?

The <!DOCTYPE html> declaration is used to define the document type and version of HTML…

By Chief Editor

What is Arrow and Normal Function in JavaScript?

Arrow Function vs Normal Function in JavaScript In JavaScript, Arrow Functions and Normal Functions (also…

By Chief Editor

You Might Also Like

JavaScriptJavaScript Interview

Difference between document.createElement and document.createElementFragement in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What is a Promise in JavaScript, and what are its parameters?

By Chief Editor
JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

By Chief Editor
Frontend InterviewJavaScript

Explain Deep Copy and Shallow Copy in JavaScript.

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance — all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach — we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today — because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?