JavaScript Variables


In Programming we use variable to store data temporarily in computer’s memory.

So, we store our data somewhere and give that memory location a name and with this name. We can read the data at the given location in future.

For example: Think about boxes you used to organize your stuff. You put your stuff in various boxes and put a label in each box. With this you can easily find your stuff. 

So variable is like a box what we put inside the box is the value that we have assigned to variable. That's the data. and the label we put on the box is the same of out variable.

Let keyword used to declare variable.

Variable called as let name ='Emil';

console.log (name);

Few rules to naming these variables:

  1. They cannot be reserved keyword. (Reserved keyboard means if, Let, else, var etc) for example. if ('Emil') So, In Name if you use If you will get error.
  2. It should be meaningful and descriptive. Ex. a, al, ar, be, b, b2 should not be used
  3. They cannot start with numbers. For example. (1name) (2name)
  4. They cannot contain a Space or - Hyphen. For Example: First-Name or First Name you have to put together. For example: firstName

            let firstName=''

first letter of the first word should be lowercase.

Every word. After first letter should be in uppercase.

This is called camel notation, which is the word we using JavaScript to name our variable.

  1. If you. Want to declare multiple variables

1st Method let firstName='Emil', lastName:'Bellarmin'; Multiple variables Separate by Comma, but it is not best practice.

2nd method. The modern and best practice we need to declare in single line, each.

let firstName='Emil'; let lastName ='Bellarmin';


Post a Comment

Share your thoughts here

Previous Post Next Post