0% found this document useful (0 votes)
17K views

Datatype and Variables

This document discusses data types and variables in JavaScript. It covers primitive data types like numeric, string, boolean, and composite data types like objects and arrays. It defines variables as named containers that store data in memory. Variables can be declared with the var keyword and have either global or local scope. The document provides naming conventions for variables and differentiates between declared, undeclared, and undefined variables. It also discusses null and undefined values as well as constants.

Uploaded by

Revati Menghani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17K views

Datatype and Variables

This document discusses data types and variables in JavaScript. It covers primitive data types like numeric, string, boolean, and composite data types like objects and arrays. It defines variables as named containers that store data in memory. Variables can be declared with the var keyword and have either global or local scope. The document provides naming conventions for variables and differentiates between declared, undeclared, and undefined variables. It also discusses null and undefined values as well as constants.

Uploaded by

Revati Menghani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Section-2:

Data Types and Variables


This section deals with different Data types, creating variable, naming, types of variables supported
by JavaScript and how to use them.

2.1 Data Types

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.

JavaScript supports different data types

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.

 A variable consists of an identifier, a scope and a specific data typ

 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.

Table: list of reserved keywords in JavaScript

break else new var


case final return void
catch for switch while
continue function this with
default if throw delete
in try do instance of
type of interface null undefined
Variable Naming Convention
JavaScript has some rules when we give the name to the variable. Following factor must be
consider while naming the variable.

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.

Variable Declaration and Initialization


 Variables in JavaScript can be defined using the keyword var

.  The equal to (=) sign is used to assign a value to a variable.

 Note that var is the keyword in JavaScript.

 Users can either, separately declare the variable and then assign values to it or straight-away
declare and initialize the variables.

Declare a new variable called numvalue,

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 y = 100; //defines a variable Y and assigns the value of 100 to it

var customer // declare the variable using the var keyword

X = 10; // use the = operator to assign a value


var x = 10; //assigning a value to a variable declaring it

var y=5; //y is a variable that holds a value of 5

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.

In expressions involving numeric and string values with the + operator.

For example, consider these statements:

var temp = "The temperature is " + 100; // returns "The temperature is 100"

var message = 20 + " days till New Year"; // returns "20 days till New Year"

But, if both operands are numbers, then addition is performed:

var a= 34; Var b = 5; var sum = a + b; // sum is 39

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?

Q.2: differentiate between local and global scope of a variable.

Q.3: What is a constant variable?

Q.4: What are the major primitive types?

Q.5: What are undeclared and undefined variables?

Q.6: What is a string?

Q.7: What are two of the benefits of using variables?

Q.8: What the factors to be considered when naming the variable

Q.9: Write any five valid variable names.

Q.10: Write any five invalid variable names.

Q.11: What is the difference between null and undefined values?

Q.12: What are composite data types?

Q.13: Fill in the blanks


(a) A variable __________ or __________ a value.

(b) To declare a variable, you use the __________ keyword.

(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.

(f) Plus sign is used to ............................ numbers and strings

You might also like