Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX

23/02/2014 Categories: Javascript. No Comments on Variables in Javascript

A variable is named memory location to store data. JavaScript is a loosely typed (or dynamically typed) language i.e., there is no need to declare the data type of a variable. The type of a variable is dynamically decided at runtime based on the value assigned. A variable can be assigned any of the five primitive type values or it can refer an object.


A variable can be local or global. A local variable is declared in a function using the keyword var. Variables declared without the var keyword is always global even if they are created in a function. Variables declared using the var keyword outside the functions are also global variables.


Naming Convention


Following are the naming conventions for variables in JavaScript:

  • Variable name cannot be a keyword.
  • Variable name must start with a letter or an underscore followed by zero or more letters, underscores or digits.
  • Variable name must not contain white spaces or punctuation marks.
  • JavaScript is case-sensitive. So, count and COUNT are different.
  • Even though use of $ sign as the beginning character of a variable name is supported by new versions of browsers, it is not recommended. 


Local Variable


Local variables are variables that are declared within a function. The general syntax for creating a local variable is:


var  variable-name = value;


The keyword var specifies that the variable is a local variable. Below are some examples for local variables:


var sum = 0;   //number

var msg = “Hello”;    //string

var flag = false;   //boolean

var month = ”Feb”;   //string


By default if no value is specified to a variable, it is treated as undefined.


Global Variables


A global variable is created without using the keyword var. The general syntax for creating a global variable is as shown below:


variable-name = value;


Below are some examples for global variables:


x = 1       //number

lang = “js”     //string

flag = true;     //boolean


If a variable is declared inside a function without using the var keyword, it is also treated as a global variable. If a variable is already declared outside the function, no new variable will be created.


typeof Operator


The typeof operator accepts a single operand either a value or a variable and returns its type as a string value. It returns “number”, “string” and “boolean” for values or variables of the type Number, String and Boolean respectively. It returns “object” for values or variables of the type object or null. For a variable which is not assigned a value, the typeof operator returns “undefined”.


The typeof operator can be used in situations where there is a need to be absolute about the type of a value of variable before proceeding further. The typeof operator either may include parentheses for the operand or may not. So, both typeof variable and typeof(variable) are same.


Below table shows the return value of typeof operator for different values:



Return Value

String ‘string’
Number ‘number’
Boolean ‘boolean’
Function ‘function’
Object ‘object’
Array ‘object’
Null ‘object’
Undefined ‘undefined’


Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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

Scroll Up