DEV Community

Cover image for Variables in JavaScript
Sanskrati Jain
Sanskrati Jain

Posted on • Edited on

4 2 1 1 1

Variables in JavaScript

JS variables are used to store data values. But unlike any other language, the datatypes of that variable are not declared first like in C++. JavaScript has dynamic types. This means that the same variable can be used to hold different data types,

  1. var, let, and const:

To declare variables we use var, let, and const.
var and let are very similar except in block scope which is discussed in the difference between var and let.
cont is the same as let but used only for constants.

Alt Text

  1. Difference Between var and let: The main difference between var and let is that scope of var is global while that of let is limited to block. This means if I declare a variable using var, it can be excessed anywhere except in the case of a function, where you can't excess a variable outside the function. This can be a problem if we use it in loops for example.

Alt Text

  1. Properties of const : People usually misunderstood the cont. It does not define a constant value. It defines a constant reference to a value, which means we can actually change the properties of a constant object or the element of a constant array. It is just you can reassign a value.

Alt Text

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (1)

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

The scope of var is not global if it's used within a function. var is scoped to the function it's declared in.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

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

Okay