Variables, Datatype and memory Concepts in Javascript


The Memory and Variable concept in JavaScript is not at all like Java as most new people think about it, but its like most of other scripting language but the memory management is really important as it might slow down your web browser. JavaScript supports int, float, char, boolean, String, object and array types.

Memory Concepts in JavaScript are nothing more than the management of its various storage elements that is to be said its variables and constants. It also includes the arrays and object, the functions also takes up some memory so we can also discuss it in the memory concept. So lets start up with the variables in JavaScript.

The variables in JavaScript are declared using the "var" keyword. you can also declare them without using var keyword but it is less common practice and is usually seen inside any function body. for example like this


function sum(a,b){
	return a+b;
}

in the above function there are two variables a and b but their scope is limited to function body only. They can not be accessed out side the function body.

You can access a general variable inside a function but not in case of shadowing. The shadowing makes it difficult for the interpreter to decide which variable was meant by the user to access by the program. for example in this case


//case 1
var a = 10;

function demo(){
	console.log(a);
}

demo();

this will output

10 

on the console, but on the other end if we do little bit of shadowing here


//case 2
var a = 10;

function demo(a){
	console.log(a);
}

demo();

this will log

undefined 

on the console. This is because the interpreter will prefer the local variable over the global variable thus the value of local variable which is undefined as of now will be logged. Now try to assign a value to the local variable and you will see the value printed there.

Anyway this was just a little bit for the startup we will soon see more of it and we will also define the ways to manage memory efficiently. But before that we would like to tell you about the values which are stubborn enough to not change themselves even if user want them to change. Well, thats not really bad of them, its the property.

Constants in JavaScript are defined using the keyword "const". just place this keyword before the declaration and it will stick to the value first assign to it.

for example


const a =10; //defining a constant
a = 20; //redeclaration error 

yes, the code will produce an error.

JavaScript memory management could be really important if only the script is getting bulky. Because in that case the browser will need more memory to run the JavaScript and ultimately it will stop working and script will hang often.

However you shall not be taking much of tension about the issue, JavaScript comes with automatic garbage collection, but remember its you who must declare a variable as free to be recollected. By marking a memory that is to say a variable, free is to set it to null or undefined.

The memory space taken by a variable in JavaScript is depending up on the value stored in it. sometimes it is more sometimes less, for example if it is an array or an object, depending upon the number of elements stored in it the size may be large on the other hand if its just a tiny little boolean variable it would hold a bit telling 0 or 1.

there are following types of data type in JavaScript:

int, float, char, boolean, string, array, object etc. The objects define much more complex structure compared to the int, float or boolean.

The int stores the whole number values. which does not contains the decimal points.
Float contains the floating point values.
Booleans are the true or false only values, the integer 0 is also equivalent to false in JavaScript.
Char is the integer type storage except for it is used to represent a single ascii character.
String is the collection of char, but JavaScript provides more detailed support for the Strings.
Arrays are the collection of multiple elements, in JavaScript you can store any type of values in an array irrespective of their type.
Objects represent the OOP concept and is usually required in the complex functionality.

 

In the following program we will show you how you can define the different types of literals in JavaScript.


var a = 10; //integer
var b = 5.505; //float
var c = 'c'; //char value
var d = 'Decided'; //string literal
var e = [3, 3.5, 'a', "hello", false]; //array literal
var f = {"a":5, "b":"hello" } //object literal

These are all different type of literals in JavaScript, In JavaScript functions are also known to possess the object properties. A function takes up the space for its required variables, the variables which has the local scope for that function. For example


function a(aa,bb){
	return aa+bb;
}

this will only require the space for its aa and bb variable and may be little more that that as a reference to store itself in memory, as soon as it returns the result it is removed from memory, but again its a call from Operating System if it is to be removed from the stack or not.

So that is quite much about the memory concepts in JavaScript. You can ask your queries and post your suggestions in the comments.