Datatype and Variables
Datatype and Variables
A data type is a classification of the type of data that a variable or an object can hold data type is an
important factor in all computer programming languages, including Visual Basic, C#, C/C++ and
JavaScript.
A) Primitive Data Types These types of data are called building blocks of a program. A single
literal value is assigned of type number, string characters. JavaScript supports three basic data types:
1.Numeric: Numeric type has two type viz. Integer and floating point. Integer is a whole number
such as 145, -9 etc. It can be expressed in decimal base(10), octal base (8), hexadecimal (16) of both
positive and negative values. Floating point are values with decimal value or fractional value. For
example 12.3, 1.5e-2 are the floating point examples. numbers in JavaScript are doubleprecision 64-
bit format. the standard numeric operators are supported, including addition, subtraction, modulus
(or remainder) arithmetic and so forth.
2. String: Strings are collection of characters which are enclosed with either single or double
quotes. A starting and ending quotes of string value must be matched. Single quotes can hide the
double quotes or double quotes can hide the single quotes. For example: "This is a beautiful Scene" '
This is a beautiful Scene ' " This is a beautiful ‘Scene’ " ' This is a beautiful “Scene "' A null string is a
string which is empty. String also contains escape sequence characters like ‘\n’, ‘\t’, ‘\b’, ‘\r’ etc. A
string can be concatenate using ‘+’ sign. For example, the expression 7 + 120results in 127, whereas
“7” + 120 results in “7120”
3. Boolean: This type of data contains only two value i.e. true or false or 0 or 1. When these
values are compared then true value is taken as 1 and false value is taken as 0. A numeric
comparison and equality operator is used for comparison.
B) Composite Data Types These types of data type are complex in nature which consist more
than one component. Object, arrays and functions are the example of composite data type. Object
contains properties and methods; array contains a sequential list of elements and functions contains
a collection of statements.
Null and Undefined Values
A null keyword represents “no value”. It is not empty string or zero, Null value is used to initialize
the object to clear the value of a variable and memory assigned to object becomes the free.
When a variable is declared without assigning value then it contains the ‘undefined’ value which
may cause the runtime error. Variables compared with these values can be compared with identity
operator
2.2 Variables
Variables can be thought of as named containers in the computer’s memory. You can place data in
these containers and refer to the value using the name.
An Identifier is the name of a variable. Usefulness of Variable When code is written they can be
used in place of unknown value. When script is written or updated then it saves the time
Then clearer the purpose of code because you can give the meaningful name which make the code
easy to read.
1. Case Sensitivity: JavaScript variables are case sensitive. When name is given to the variable then
one must be careful to use same case in the JavaScript otherwise JavaScript consider it as new
variable and return the error. For example numvalue, Numvalue, numValue NUMVALU are four
different variables.
2. Use Valid Characters: A variable must begin with underscore or letter only. All other characters
are invalid to start the variable name. Variable must not contains blank spaces. For example,
_numvalue, num2value, numvalue are valid name while 2numvalue, _num value, num value are
invalid name.
3. Avoid Reserved Words: Reserved words are the special words which has specific meaning in
JavaScript. These words cannot be used as a variable name. For example ‘while’ is a reserved
keyword hence cannot be used as variable name.
Users can either, separately declare the variable and then assign values to it or straight-away
declare and initialize the variables.
Write the following: var numvalue; Note that the semicolon at the end of the line is not part of the
variable name but instead is used to indicate to JavaScript the end of a statement.
Examples of variables
var X; //defines a variable X, and by default no value is assigned to this variable
var sum = x + y ; // sum is a variable that stores the sum of variables x and y
Variable Scope
The scope of a variable refers to the area of a program in which it can be accessed.
Variables have two types of scopes -global scope and local scope.
The global scope: consider the example while handling the rate of sugar, we can declare a variable
Sugar Rate at global. It is fixed in the fair price shop by the government and is same throughout the
state in all the fair price shops. This is an example of a variable named sugar rate being declared
globally.
The local scope: in case of local scope the rate of sugar in different retails shops will be specific to
the rating norms of each shop. This is an example of a local variable. It is specific within the scope of
each shop.
Concatenation of variables
To concatenate variables and strings together on the same line, the + sign is used. Addition is
performed when both of the operands are numbers.
var temp = "The temperature is " + 100; // returns "The temperature is 100"
var message = 20 + " days till New Year"; // returns "20 days till New Year"
Declaring Constants A constant is a special variable with a value that cannot be changed during
program execution. JavaScript declares constants with the const type and the name of the constant
is in uppercase by convention only. Many browsers support the JavaScript constant variable.
Following statements declare the PI and INTEREST constant variable.
const PI = 3.14;
const INTEREST = 3;
Answer the following questions:
Q.1: What is a variable?
(c) A variable name must begin with a(n) __________ or a(n) __________ character.
(d) A variable name must begin with a(n) __________ or a(n) __________ character..
(e) To denote an exponent in JavaScript, you use a letter __________ right after the base number
and before the exponent.