Chaduvula Prasanth

Navigate back to the homepage

Intro to Javascript variables

Chaduvula Prasanth
December 8th, 2019 · 2 min read

In any product, we have to store the information that belongs to the product.

we have Rs.100/-, we need a locker to store that Rs.100/- and a bank should make that locker for us.

In Javascript, it works in the same way:

To store Rs.100/- we need a variable and we can make variable by using var, let and const.

we can store any type of information in a variable. Think of a variable as a box which stores any type of information.

1var locker; // this statement creates a variable and named it as a locker.

In above statement var is a bank which maker locker for us.

1var locker = 100; // here we declared a variable named locker and assigned 100 to it.

The other way of doing it.

1let locker = 100;
2const locker = 100;

we can type variable name to access the value stored in it. if we type locker it gives us value 100

To change the value 100 to 200 we can simply write

1locker = 200; // here we reassigned value 200 to variable locker

we can declare a new variable and assign another variable value to it

1var another = locker; // here we declared a variable named other and assigned value of locker

To check enter

1another; // which shows us 200 as value

About var, let and const

var:

  1. We can declare a variable & assign a value
  2. We can redeclare a variable & reassign a value
  3. With var, it initializes the value of undefined in declaration mode & it assigns value in execution mode

let:

  1. We can declare a variable & assign a value
  2. We can reassign a value
  3. With let, it won’t initialize any value in declaration mode and It assigns value in execution mode

const:

  1. We can declare a variable
  2. We can assign a value
  3. With const, it won’t initialize any value in declaration mode and It assigns value in execution mode

We can also declare multiple variables in one line:

1var name = "stark",
2 age = 22,
3 gender = "male";

The above statement normally we can write like

1var name = "stark";
2var age = 22;
3var gender = "male";

Variable naming: There are two limitations on variable names in JavaScript: The name must contain only letters, digits, or the symbols \$ and _. The first character must not be a digit. eg:

1var firstName = "bran stark";
2var you123 = 45;
3
4var 123test = "bran stark" // invalid because we can’t use number as first charcter;
5var first-name = "bran stark" // invalid because we can’t use hyphen in variable names

If we want to declare names with more words normally we follow camelCase. eg:

1var yourFirstName;

In naming variable case matters. if two variables with the same name but different case are different variables eg:

1var name;
2var Name; // it is different variable from above

Reserved names: We can’t use some names like function, new, let, class … and there so many, we will find in javascript laterin our learning phase

Naming things in the right way: Always a variable name should be clean, readable and describing the data it stores. for example, we want to store a name in a variable.

1var a = "bran stark"; // wrong convention
2var userName = "bran stark"; //meaningful

Variable naming is one of the most important and complex skills in programming. A glance at variable names can reveal which code was written by a beginner versus an experienced developer.

More articles from Chaduvula Prasanth

Travelling 470 km per day on 100 cc bike to the last land of India - part1

A road trip from Madurai to Dhanushkodi (the last land of India) with my best friend on Hero Hf Deluxe 100cc bike.

November 24th, 2019 · 1 min read

How to start your startup journey from prototype to MVP and product iterations.

90% of startups fail in their first five years but the reaming 1% will fly. To start anything you have to leave your comfort zone first…

November 23rd, 2019 · 1 min read
© 2020 Chaduvula Prasanth
Link to $twitterLink to $githubLink to $linkedin