java

Variables Defined

Variables in a programming language are similar to those in Mathematics in one manner: a variable is some named entity that has no fixed value. The digit 9, for example, is not a variable because it has a fixed value. On the other hand, we can say x=6 is a variable because it can be re-assigned a new value whenever required. A variable is important only when it has been assigned a value.

Variable Naming Rules

As computers work with many types of data (alphanumeric, digits, images, etc.), the variables in a programming language can represent anything from an alphanumeric string to an image. In JavaScript, the name of a variable should start with an alphabet followed by any combination of alphabets, digits and underscores. Thus totalCost and day5 are legal while 5day and total@Cost are illegal variable names. Another important thing to note is that JavaScript is case sensitive. Thus, totalCost refers to a different variable then both TotalCost and totalcost. With regard to variables two things are very important: Scope and Data Type.

Scope of a Variable

Scope is defined as the visibility of a variable. If you don’t know how to write JavaScript functions then understanding scope may be difficult for you. Leave it if you feel your head spinning. Programming languages usually allow two broad types of variables: local and global. A local variable is one that can be used in a single function. If you try to change / read the value of a local variable from outside that function, an error will be reported. A global variable, as the name suggests, can be read / changed from any function on a single page. You don’t need to do anything special to incorporate scope of a variable – its automatically handled for you. A variable that is defined in a function (we will see how to define a variable shortly) is local. A variable defined outside any function is global. These logical concepts are simply for making you a better programmer.

Using Variables

A variable is defined as var variableName;. The part var and the semicolon are part of the syntax and will always be used like this. variableName is the name of the variable that you want to define. Values are assigned as variableName = value. For example to mean “let i be equal to 10”, we write i=10;. Some examples are given below:
 var name;
	name = "Muhammad Ali Shah"; // alphanumeric
	var i;
	i = 5*10; // an integer

Data Types

JavaScript is a loosely typed language – you don’t have to define what type of contents a variable can have and JavaScript automatically handles conversions between different data types. For example suppose you want to write i = 5.0 * 1. In this case, 5.0 is a real (recall your Arithmetic lessons) and 1 is an integer. JavaScript automatically handles the conversion from integer to real for multiplication. You may be surprised to know that certain languages do not allow such operations! Although you will never define the data type of a variable in JavaScript, knowing various categories of variables is very helpful:
  • Numbers: Used to represent both reals and integers. In computer science, a real is referred to as a floating point number. JavaScript allows integers to be written in decimal, octal or hexadecimal notation. A hexadecimal number starts with 0x. For example, 9A will be written as 0x9A. Similarly, an octal number starts with a leading 0. Floating point (real) numbers contain a decimal point and may also cotain an “e” or “E” to represent scientific notation. For example, 3.8 into 10 raised to the power 5 can be written as 3.8E5.
  • Booleans: A boolean variable can take only two values – true and false. Unlike C++, Java and JavaScript differentiate between a 0 and a false. Boolean variables are important for evaluating conditions.
  • Strings: Strings are the same as alphanumeric text. JavaScript allows you to concatenate two strings by using a plus operator. For example, you can make “Hello World” by writing “Hello” + ” World”.
  • Undefined: A variable is said to have an undefined data type when it has been defined using a var statement but not given any values – that is, we cannot tell the data type of totalCost by seeing var totalCost.
  • Objects: JavaScript has several built-in objects for you to use. To create a variable myObject to point to a built in image type, we write myObject = new image(). More on this later.
JavaScript is a loosely typed and case sensitive scripting language. It also differs in a local and global variables. JavaScript provides automatic conversion between variables and constants of different data types and you don’t declare the data type of a variable while defining it. A variable is defined by a var statement.