There are majorly two types of languages. First, one is Statically typed language where each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types.Example: C, C++, Java.
// Java(Statically typed)
int x = 5 // variable x is of type int and it will not store any other type.
string y = ‘abc’ // type string and will only accept string values
var x = 5; // can store an integer
var name = ‘string’; // can also store a string.
The latest ECMAScript(ES6) standard defines seven data types: Out of which six data types are Primitive(predefined).
- Numbers: 5, 6.5, 7 etc.
- String: “Hello GeeksforGeeks” etc.
- Boolean: Represent a logical entity and can have two values: true or false.
- Null: This type has only one value : null.
- Undefined: A variable that has not been assigned a value is undefined.
- The value stored in a variable can be changed during program execution.
- A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
var var_name; var x;
Notice in the above code sample, we didn’t assign any values to the variables.We are only saying they exist.If you were to look at the value of each variable in the above code sample,it would be undefined.
// declaring single variable var name; // declaring multiple variables var name, title, num; // initializng variables var name = "Harsh"; name = "Rakesh";
// creating variable to store a number var num = 5; // store string in the variable num num = "GeeksforGeeks";
// storing a mathematical expression var x = 5 + 10 + 1; console.log(x); // 16
After ES2015,we now have two new variable containers : let and const. Now we shall look at both of them one by one. The variable type Let shares lots of similarities with var but unlike var it has scope constraints. To know more about them visit let vs var. Let’s make use of let variable:
// let variable let x; // undefined let name = 'Mukul'; // can also declare multiple vlaues let a=1,b=2,c=3; // assignment let a = 3; a = 4; // works same as var.
Const is another variable type assigned to data whose value cannot and will not change throught the script.
// const variable const name = 'Mukul'; name = 'Mayank'; // will give Assignment to constant variable error.
Scope of a variable is the part of the program from where the variable may directly be accessible.
- Global Scope – Scope outside the outermost function attached to Window.
- Local Scope – Inside the function being executed.
Let’s look at the code below. We have a global variable defined in first line in global scope. Then we have a local variable defined inside the function fun().
When we execute the function fun(), the output shows that both global as well as local variables are accessible inside the function as we are able to console.log them. This shows that inside the function we have access to both global variables (declared outside the function) and local variables (declared inside the function).Let’s move the console.log statements outside the function and put them just after calling the function.
We are still able to see the value of the global variable, but for local variable console.log throws an error. This is because now the console.log statements are present in global scope where they have access to global variables but cannot access the local variables.