Variables and Data Types
Variables and Data Types
Topic:JavaScript - Variables
JavaScript Variables
JavaScript variables are containers for storing data values. Variables can be thought of as named
containers.we can place data into these containers and then refer to the data simply by naming the
container.
Before we use a variable in a JavaScript program, we must declare it. Variables are declared
with the var keyword as follows.
<script type = "javascript">
var money;
var name;
</script>
var a = 12;
var b = 10;
var c = a + b;
we can also declare multiple variables with the same var keyword as follows −
<script type = "text/javascript">
</script>
1
var money;
money = 2000.50;
</script>
[Note − Use the var keyword only for declaration or initialization, once for the
life of any variable name in a document. we should not re-declare same variable
twice.
JavaScript is untyped language. This means that a JavaScript variable can
hold a value of any data type. Unlike many other languages, we don't have to tell
JavaScript during variable declaration what type of value the variable will hold.
The value type of a variable can change during the execution of a program and
JavaScript takes care of it automatically.]
JavaScript Identifiers:All JavaScript variables must be identified with unique
names. These unique names are called identifiers. Identifiers can be short names like a
and b or more descriptive names like mark1, mark2, total, age, sum, total volume.
The general rules for constructing names for variables are:
• Names can contain letters, digits, underscores, and dollar signs.
• Names must begin with a letter
• Names can also begin with $ and _
• Names are case sensitive (a and A are different variables)
• Reserved words like JavaScript keywords cannot be used as names
. For example, 123test is an invalid variable name but _123test is a valid one.
NOTE − Care should be taken while writing variable and function names in JavaScript.
2
<body onload = checkscope();>
<script type = "text/javascript">
</script>
</body>
</html>
3
default if return volatile
double in super
JavaScript Datatypes
One of the most fundamental characteristics of a programming language is the set of data types
it supports. These are the type of values that can be represented and manipulated in a
programming language.
Primitive Data:A primitive data value is a single simple data value with no additional
properties and
JavaScript allows us to work with three primitive data types −
1. Numbers(Primitive) JavaScript has only one type of numbers. Numbers canbe
written with or without decimals. , eg. 123, 120.50 etc. Numbers are written without
quotes.
Example: var side = 10; // Number
var num1 = 87.0; // Written with decimals
var num2 = 87; // Written without decimals
Extra large or extra small numbers can be written with scientific (exponential)
notation:
Example 19
var exp1 = 232e5; // result is 23200000
var z = 123e-5; // result is 0.00232
4
e.g. true or false.
4. Undefined: In computer programs, variables are often declared without a value. The
value can be something that has to be calculated, or something that will be provided
later, like user input. A variable declared without a value will have the value undefined.
In JavaScript, a variable without a value, has the value undefined. The typeof is also
undefined.
Example 24
var bike; // Value is undefined, type is undefined
[Note : Any variable can be emptied, by setting the value to undefined. The type
will also be
undefined.]
Empty Values: An empty value has nothing to do with undefined. An empty string has
both a legal value and a type.
Example 25
var bike = “” ; // The value is “”, the typeof is “string”
Example:
var personName = {firstName:”Harini”, lastName: ”Kumar”, age:13,
height:”155 cms”};
personName = undefined; // Now both value and type is undefined.
5.Object(Composite Data Type): JavaScript objects are written with curly braces.
Object properties are written as name:value pairs, separated by commas.
Example: var x = {firstName:”Harini”, lastName:”Kumar”}; // Object
Example:
var personName = {firstName:”Harini”,lastName: ”Kumar”,
age:13,height.”155 cms”};
Summary:
1.JavaScript also defines two trivial data types, null and undefined, each of which defines only
a single value.
2. In addition to these primitive data types, JavaScript supports a composite data type known
as object.
Note − JavaScript does not make a distinction between integer values and
floating-point values. All numbers in JavaScript are represented as floating-point
values. JavaScript represents numbers using the 64-bit floating-point format
defined by the IEEE 754 standard.
Dynamic data types: JavaScript has dynamic types. This means that the samevariable can
be used to hold different data types:
5
Example 15
var z; // Now z is undefined
z = 10; // Now z is a Number
z = “Sakthi”; // Now z is a String