Wednesday, 28 March 2018

Variable In JavaScript

Variable means whose value can be changed.

Variable contains value which is stored in memory and holds some space.

var keyword is used to declare a variable in javascript.


2 steps to use variables

·        Variable Declaration


·        Variable Initialization


   Variable Declaration:

Syntax: 

var variable_name;

Eg 1:

var x;

Where x is the variable name.

Variable Initialization:

Syntax:

var variablename=value;

 Eg 1:

var x=89;

Where x is the variable name and 89 is the value which assigned to x.

 

Rules for defining variables

It can have alphabets, digits and underscore  or dollar( $ ) sign.

A variable name should start with alphabet and underscore only. It can't start with digit.

No white space is allowed within variable name.

JavaScript variables are case sensitive, for example y and Y are different variables.

 

Valid variable declaration:
int x;    

     int _xy;    

      int x45;    


Invalid variable declaration:
 int 7;   
 int *bb  


Example:

   <script>  
   var x = 20;  
  document.write(x);  
  </script>

Types:
There are two types of variables in JavaScript ,they are
 1)local variable
 2)global variable

Local variable:
local variable is declared inside block or function. It is accessible within the function or block only.
Example:
    <script>  
   function display (){  
   var x=20;//local variable 
    document.write("x is:"+x);   
      } 
    display();
    </script>  
    
    global variable
global variable is accessible from any function. A variable i.e. declared outside the function or declared with window object is known as global variable.
Example:
<script>  
var x=30;//global variable  
     function display(){  
     document.write("x is:"+x);    
        }  
    display();
  </script>  

1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...