DEV Community

Cover image for Var vs Const vs Let (JavaScript)
Hardik Mirg
Hardik Mirg

Posted on • Edited on

3 1

Var vs Const vs Let (JavaScript)

Variable Declarations

There are several ways of declaring values to variables in javascript:

  • Var
  • Const
  • Let

Var

var stands for "variable" is used to declare variables that can be reassigned and are only available inside the function they're created in. They're function scoped.

var word = "hello"
console.log(word) // returns "hello"

word = "bye" // can be re-assigned ✅
console.log(word) // returns "bye"
Enter fullscreen mode Exit fullscreen mode

Const

const stands for "constant" and is used to declare variables that cannot be reassigned and are not accessible before they appear within the code. They're block scoped.

const word = "hello"
console.log(word) // returns "hello"

word = "bye" // cannot be re-assigned ❌
console.log(word) // throws an error as constants cannot be re-assigned
Enter fullscreen mode Exit fullscreen mode

Let

Variables declared using let can be reassigned but are similar to const i.e. block scoped. If variables are not created inside a function or block they are globally scoped.

  • Block

    A block is a set of opening and closing curly brackets.

Hope you learned something useful today! Peace Out ✌️

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay