Javascript Variables – How The Javascript Stores The Data


Variables are the basic fundamental concept in any programming language and in javascript too.

“In Javascript, we use variables to store the data. The data can be any form like strings, numbers, arrays, objects, and even functions.”

In simple words, variables are like containers. How the containers store different types of things likewise variables store different kinds of data like strings, numbers, booleans, arrays, objects etc.

A javascript variable is nothing but a container, which is used to store the data or value.

Once you declare a variable, you can use it over and over again in your script. But the scope rule applied.

We will discuss scope later in this tutorial.

Declaring javascript variables.

Before you use a variable in javascript you need to declare it.

In javascript, we create variables using var keyword.

Below is the example, how you can declare a variable with var keyword in javascript.

Ex: var name;

In the above example, we create a variable is using the  var keyword.

But, it has no value.

If you don’t specify any value to a variable it will return"undefined"

You can specify a value to a variable using "="  ( equals to ) operator. see the example below.

If you want to know more about operators, here is the complete tutorial on javascript operators.

diagram of declaration a javascript variable

In the above example, name is the variable name and  = ( equals to ) is an operator. And  "Jhon doe" is the value assigned to a variable.

You can create multiple variables at the same time using var keyword.

 var name,age,place;

In the above example, I created 3 variables using a single var keyword.

You can create as many as you want.

But each one should be separated with a comma(,).

You can also assign a value to a variable while declaring it.

var name = ' Jhon doe ';

Declaring multiple variables with values

var name='jhon',age=34,place='Hyderabad';

Variable types in javascript

If you have used before any statically typed programming languages like C, C++, Java. Then you may notice that variable declaration starts with an associated data type like int,varchar,float etc…

But, In javascript, there are no associated data types. We create variables using var keyword.

In javascript, you can store any type of data in a variable. And you can override any value with any type, and it is legal.

For example, below I declare a variable x and stores the value number 10

var x = 10;

And later I can re-assign the value to x with any type.

For example x = "Hello World";

Now the value of x will be Hello World

In statically typed languages this type of variable conversion will throw an error, but in javascript it is legal.

The scope of a javascript variables

” The visibility (from where to where the variable can be accessed ) of a variable is known as variable scope “.

There are 2 types of scopes in javascript.

  1. Local scope
  2. Global scope

A local variable have local scope and a gloabl variable have global scope.

Local variables are declared within the function and are accessible only within the body of the function.

var localScope = function(){
   
    var name = "Jhon doe"; // local variable it is accessible to this function only
    console.log(name); //accessing a local variable

}
console.log(name); // trying to access a variable which has a local scope.
// prints undefined.

The scope of the variable name is within the localScope function only. Whenever I try to access the variable name from outside of the function, it will prints undefined.

You can declare and use global variables anywhere in your javascript code.

You may omit the var keyword while declaring global variables.

name = 'Jhon doe'; // can be accessed any where

var globalScope = function(){

   console.log(name);

}

console.log(name);

Below are the examples of how you can use variables to store different types of values.

var name = 'Jhon doe'; //string variable
var age = 20  // number variable
var on = true // boolean variable
var marks = [1,2,3,4,5] // array variable
var student = { name:'jhon',age:20,city:'new york' } //object variable
var result = function(){
       
     console.log( " A block buster movie " );

}   // a function variable.

If you want to learn more about javascript, below I listed the free ebooks. Have a look.

  1. Eloquent Javascript
  2. The Javascript Way
  3. Javascript For Cats
  4. Speaking Javascript
  5. Thinking In Javascript

If you have any doubts feel free to comment below. If you like this article, please share with your friends.


Leave a Reply

Your email address will not be published. Required fields are marked *