DEV Community

Sashank Rampalli
Sashank Rampalli

Posted on

Variables in JavaScript

Variables are used to store data on a computer.

There are different type of data that you can store in a variable, the following are different types of data types:

  1. string
  2. number
  3. boolean
  4. object
  5. null
  6. undefined
  7. symbol
  8. BigInt
  9. function

Variables are accessed with a label, not with the data itself.

Let me explain the above point, firstly to create a variable you need to use the "var" keyword followed by a name for the variable and then assigning a value to it from any of the above data types.

Ex: var count = 20;

Here "count" is called the label. When we need to use the variable later we use its label to get the value stored in it.

Here we have stored a number 20 in the variable with a label count.

Hope this has helped you understand how to use variables.

Top comments (4)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Types - you missed BigInt and Function

Collapse
 
sashankr profile image
Sashank Rampalli

Hi Jon,
Thanks for correcting me, I have added them now.
I appreciate your feedback.

Collapse
 
mshajid profile image
mshajid

Sorry No offense, Am I the only who using let & const in 2020? Why still using "var"? To be honest, I'm also a newbie although, just let me know what is the core differences between them using in 2020?

Collapse
 
sashankr profile image
Sashank Rampalli

Yes let and const are part of es5 and they are used in modern JS code, however, there is still a lot of old code written with var.

I am sharing the basics of variables from Vanilla JS.
Well, const is mainly used to prevent reassigning and overriding its value.
let is used mostly in loops because the counter value has to be reassigned every time.