Monday, 2 Jun 2025
  • My Feed
  • My Interests
  • My Saves
  • History
  • Blog
Subscribe
Code Reveals
  • Home
  • HTML

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

    By admin

    What is Doctype HTML in HTML?

    By admin

    What is a Meta Tag in HTML?

    By admin
    What is Symentic HTML

    What is Symentic HTML?

    By admin

    Difference between HTML Tag and HTML Element in HTML?

    By admin

    What are the different types of HTML tags?

    By admin
  • JavaScript
    What are the Lexical Scope in JavaScript

    What are the Lexical Scope in JavaScript?

    By admin

    How to Reverse a String in JavaScript: Two Essential Methods

    By admin

    Explain Call, Apply and Bind in JavaScript.

    By admin

    What is Callback Hell in JavaScript?

    By admin

    What are the map, filter, and reduce methods in JavaScript?

    By admin

    What is Scope in JavaScript?

    By admin
  • Frontend Interview

    What is Block level Element and Inline Level Element?

    By admin

    Difference Between position: relative and position: absolute in CSS

    By admin

    What is Position in CSS?

    By admin

    What is Symentic HTML?

    By admin

    Is JavaScript a synchronous or asynchronous language?

    By admin

    Explain Deep Copy and Shallow Copy in JavaScript.

    By admin
  • Backend Interview

    Mastering Star Rating Systems: Best Practices and Optimized Code Examples

    By admin

    Explain var let and const in JavaScript with Example.

    By admin

    What are Benefits in React?

    By admin

    Difference between HTML Tag and HTML Element in HTML?

    By admin

    How Does React Work?

    By admin

    What are the drawbacks of React?

    By admin
  • Other
    • Contact Us
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Submit a Tip
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Code RevealsCode Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • Pages
    • Home
    • Blog Index
    • Contact Us
    • Search Page
    • 404 Page
  • Features
    • Post Headers
    • Layout
  • Personalized
    • My Feed
    • My Saves
    • My Interests
    • History
  • About
  • Categories
    • Technology
  • Categories
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.

admin
Last updated: February 16, 2025 5:36 pm
admin
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

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

Difference Between document.createElement() and document.createDocumentFragment() in JavaScript 1. document.createElement() This method creates a single HTML…

By admin

What is the this Keyword in JavaScript?

The this keyword in JavaScript refers to the object that is executing the current function.…

By admin

What is localization in React?

Localization (l10n) in React refers to the process of adapting your application to support multiple…

By admin

You Might Also Like

JavaScriptJavaScript Interview

What is Callback Hell in JavaScript?

By admin
JavaScriptJavaScript Interview

What are the map, filter, and reduce methods in JavaScript?

By admin
JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

By admin
JavaScriptJavaScript Interview

What is one-way data binding in React?

By admin

Code Reveals is a cutting-edge software development company dedicated to delivering high-quality, scalable, and innovative solutions for businesses of all sizes. Our team of expert developers, designers, and engineers specializes in creating custom software, web applications, mobile apps, and enterprise solutions that are tailored to meet the unique needs of our clients.

Most Famous
  • HTML
  • CSS
  • JavaScript
  • Node
Top Categories
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
Usefull Links
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Submit a Tip

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?