Variables - Javascript



In Javascript variables can be declared in three ways using 

// var, let, const

How to declare a variable using var ?

var name = "Jack";
var place = 'Sydney';

Note: you can use single quote or double quote to declare a string.


You can reassign value to same variable like this

var name = 'John';
console.log(name); // it will print John
name = 'Jack';
console.log(name);  // it will print Jack 


What will happen if you initialize a variable but don't assign it any value ?

var name;
console.log(name); // You will get undefined in browser console


What are the naming convention for a variable name ?

1. Variable name can contain letters, number, _ and $

2. Variable name can not start with a number

3. variable name can start with $ sign but it is not recommended , since jquery a very popular javascript library use it.

So let's look at some examples of allowed and not allowed naming convention

var 1name = 'jack'; // Not allowed, invalid syntax since you are starting variable name with a number
var $name = 'john'; //allowed but not recommended
var name1 = 'Henry'; // Absolutely ok , since variable name don't start with a number


What are the naming convention for multi word variable names?

1. Camel Case

var countryCity = 'India Delhi';

2. Underscore

var country_city = 'India Delhi';

3. Pascal case

var CountryCity = 'India Delhi';


So far we have discussed about how to use var, now let's discuss the use of let

 let is more or less same as var, but the importance of let will be visible when we will discuss about local and global scope in functions. So for now just understand you can use let in place of var in all above example.

let name = 'John';
console.log(name); // it will print John
name = 'Jack';
console.log(name);  // it will print Jack 



Now let's discuss how to use const, any variable you define using const can't be changed or reassigned basically const mean constant. 

Another important thing you need to take care while using const is that you have to assign a value to variables defined using const, you just can't leave them undefined

const city = 'Shangai';
city = 'Mumbai';  // this will throw an error , since it is a const and hence can't be reassigned

const country; // this will throw an error since you haven't assigned any value to this variable


One last thing to understand about const, (which very often confuse people new to javascript) is that you can't reassign new values to const variables but you can mutate these variables. For example we can change values in objects or lists (Although we haven't yet formally studied javascript objects and lists yet in this course still i'll assume you will be able to follow what i am trying to explian , if it seems like it is going over from your head don't panic just skip this part, we will deal with this in later chapters)

const address = {
    "city": "delhi"
}; = "mumbai"; //this is an absolutely valid syntax

const score = [12,5,11];
score.push(13);  // here also we are mutating a list which is valid