How to declare variables in javascript? 🤔
Tags: javascript, beginners, writing
Published: 2021-01-16 03:45:34

Hi...!
In this article, we are discussing variables. There are different declared variables in different programming languages. Javascript is very freely declaring variables.

![Instagram post - 1variable](https://dev-to-uploads.s3.amazonaws.com/i/5e0hszxbptojszy9opqf.png)

There is some different way to declare in javascript. But First
#What are the variables?
![Instagram post - 2variable](https://dev-to-uploads.s3.amazonaws.com/i/vk96uyo1pixs9e3om7kv.png)

Now you think about your purchase gift box. But the gift boxes are empty there is no value 😅. It looks like the above image ☝️ 
There is no value. 
 
![Instagram post - 3variable](https://dev-to-uploads.s3.amazonaws.com/i/xfzsww1ixdkjm0eo1k30.png)

Now you put a toy in the gift box 🎁. The gift box is valued because you put a toy in the gift box. if you open the box you see there is a toy. 🤩 
Here gift box we are call variable name and toy we are called value.
`variable name is = gift box`
`variable value is = toy`

the variable name holds the data and stores it.

#How do I declare variables in javascript? can you tell me?
In javascript, you can declare variables in 3 ways.
1. using var
2. using let and
3. using const

#var

![Instagram post - 6variable](https://dev-to-uploads.s3.amazonaws.com/i/d1dqhxm71my71b89tvnx.png)
 

First, we are trying with `var` variables. So `var` is stores data in memory for example
`var fullName = 'Kuldeep';` using var keyword you are use-value anywhere in the program. Now you are using `fullName` anywhere in the howl program like `console.log(fullName);`🤪

#let

![Instagram post - 4variable](https://dev-to-uploads.s3.amazonaws.com/i/bazqirtm5naqigur5lv2.png)

Now second is the Let keyword.

Let keyword you also store data in javascript program. There are lots of think with the let keyword. Because with let you are store value multiple in one variable. 
For example

`let message;`
`message = 'Hello kuldeep 👋';`
`console.log(message);`

There are messages that hold one think Hello Kuldeep 👋. But if you want to store data in one variable you can also do that.

`let user = 'Kuldeep', age=25, message = 'Kuldeep Hello' `
`console.log(user);`
`console.log(age);`
`console.log(message);`
In older scripts, you may also find another keyword: var instead of let:
`var` and `let` work as same but var are the old way to declare now a new way to hold store data `let` keyword.

But if you are declaring a variable already and you have put another variable value what happens when you try like this.
`let fullName = 'Kuldeep';`
and now 
`let fullName = 'Jone'`;

###What happened when you run 👆 in this program.
There are show errors 🥴. O nooo because fullName is already declared in the program
![Screenshot_2021-01-13_10-39-55](https://dev-to-uploads.s3.amazonaws.com/i/v3wn9ch6g1p5eeuubvua.png)

I have an error in the console. 
Uncaught SyntaxError: Identifier 'fullName' has already been declared

Now we are talking about rules about how to declare a variable and variable name.

![giphy (1)](https://dev-to-uploads.s3.amazonaws.com/i/6u2kvvak5hkzyy50ori0.gif)

1st: Variable name does not start with a number and # keyword.
`var 1name='kuldeep';`❌
`var #name= ''`❌
👆 this is wrong to declare a variable in javascript.

2nd: Variable name start with `A-z, $, _`
`let firstName = 'Oliver';`✅
`let _secondName = 'kuldeep';`✅
👆 this Right way to declare a variable

3rd: You are not using `-` in javascript
`let first-name='Jone';`❌
👆 this is wrong to declare a variable in javascript.

4th: CaseSensitve word.
`let first = "kuldeep";` and you write `let fiRsT ='Oliver';`
👆 There is two different variable name first and fiRsT are to the different thing in Javascript.

5th: We do not use reserved keywords in javascript
`var let='kuldeep';`❌
`let const = 'Oliver';`❌


#Const

 ![Instagram post - 5variable](https://dev-to-uploads.s3.amazonaws.com/i/5tyr4kicrwo2xwbeqpaf.png)

const keyword also works as let keyword. 
If you have any queries about this article you can comment below