Page 293 - Web Applications (803) Class 11
P. 293
JavaScript statements consist of variables, keywords, operators, expressions and comments. Let us learn about them
in detail.
Variables
A variable is a name of storage location. It stores a value that can be modified later. JavaScript is a loosely typed
language which means it does not require a data type to be declared before using it. An Identifier is the name of a
variable. Syntax to declare a variable in JavaScript is:
var <variable name> = <value>;
where var is a keyword, <variable name> is a valid name for the variable and <value> is the value that you want to
assign the variable.
For example:
var age = 16;
var name = "A"
We can declare multiple variables in the same line in the following way:
var v1 = 1, v2 = 'Delhi', v3;
When declaring a JavaScript variable, there are some guidelines to follow:
Ð ÐThe variable name must begin with a letter (a to z or A to Z) or underscore(_). No spaces are allowed. Examples are
sal, _pf, total_salary.
Ð ÐWe can put digits (0 to 9) after the initial letter, for example data1, data2, etc.
Ð ÐVariables in JavaScript are case sensitive, so a and A are two separate variables.
Ð ÐVariable names can contain letters, digits, or the symbols $ and _.
ÐVariable names cannot contain blank space.
Ð
Ð ÐVariable name cannot be a reserved keyword.
Ð ÐVariable names cannot contain blank space.
Scope of Variables
A variable’s scope refers to the area of a program where it can be accessed. There are two types of scopes for variables
which are as follows:
Ð ÐLocal scope: Variables defined in local scope have a limited lifespan, meaning they can only be utilised within the
functions that define them.
Ð ÐGlobal scope: Variables defined in global scope can be utilised by any function without having to pass them as
parameters.
Concatenation of Variables
The + sign is used to concatenate variables and strings on the same line. When both operands are numbers, addition
is performed. When using the + operator in expressions involving numeric and string values, the two values are
concatenated or joined. Following examples shows the use of + sign:
var price = "The price of a pencil is " + 10;
// will display "The price of a pencil is 10"
var info = "I am " 16 + " years old.";
// will display "I am 16 years old."
If both the operands are numeric, then the + sign performs an addition operation.
Introduction to Dynamic Websites Using JavaScript 291

