FOP- Java Script_Unit 2 (1)
FOP- Java Script_Unit 2 (1)
Java Script
By:
Dr. Khyati R Nirmal
Associate Professor
Department of Computer Engineering
SNJB’s LS KBJ CoE, Chandwad
Unit 2
Data Types and Variables
Syllabus
• JavaScript’s Primitive Types- Numbers, Hexadecimal Literals, Octal
Literals, Special Values, Data Representation Issues, Data
Representation Issues, Strings, Undefined and Null; Composite
Types-Objects, The typeof Operator, Type Conversion, Variables.
• Operators, Expressions, and Statements- Statement Basics,
Whitespace, Termination: Semicolons and Returns, Blocks.
Operators- Assignment Operator, Arithmetic Operators, Bitwise
Operators, Bitwise Shift Operators, Increment/Decrement, Logical
Operators, void Operator, Object Operators
• Core JavaScript Statements- if Statements, switch, while Loops, do-
while Loops, for Loops, for Loops, Object-Related Statements,
Object Loops Using for… in
JavaScript’s Primitive Types
JavaScript supports five primitive data types:
• Number
• String
• Boolean
• Undefined
• null
JavaScript’s Primitive Types
• These types are referred to as primitive types because
they are the basic building blocks from which more
complex types can be built.
• Of the five, only number, string, and Boolean are real
data types in the sense of actually storing data.
• Undefined and null are types that arise under special
circumstances.
JavaScript’s Primitive Types: Numbers
• JavaScript includes both integer and floating-point values.
• All numbers are represented in IEEE 754-1985 double-precision
floating-point format.
• This representation permits exact representation of integers in
the range –253 to 253 and floating-point magnitudes as large as
±1.7976 x 10308 and as small as ±2.2250 x 10-308.
• Numeric literals in JavaScript can be written in a wide variety of
ways, including scientific notation. When using scientific
notation, the exponent is specified with the letter e (which is
not case-sensitive).
JavaScript’s Primitive Types: Numbers
• Formally (according to the ECMA-262 grammar), decimal literals
have one of the following three forms (parentheses indicate
optional components):
• DecimalDigits.(DecimalDigits)(Exponent)
• DecimalDigits(Exponent)
• DecimalDigits(Exponent)
JavaScript’s Primitive Types: Numbers
• In plain English, this means that all of the following are valid ways
to specify numbers:
• 10
• 177.5
• -2.71
• .333333e77
• -1.7E12
• 3.E-5
• 128e+100
• Note that you should not include leading zeros in your integers. The
reason is that JavaScript also allows numeric literals to be specified
in bases other than ten (decimal).
JavaScript’s Primitive Types: Numbers
• Hexadecimal
• Programmers often find it convenient to write numbers in hexadecimal (base-
16) notation, particularly when performing bitwise operations.
• JavaScript’s hex syntax should be familiar to readers with previous
programming experience: a leading zero, followed by the letter x (not case-
sensitive), followed by one or more hexadecimal digits.
• Hexadecimal digits are the numbers zero through nine and letters A through F
(not case-sensitive), which represent the values zero through fifteen.
• Eg.
0x0
0XF8f00
0x1a3C5e7
JavaScript’s Primitive Types: Numbers
• Octal Literals
• Octal literals begin with a leading zero, and octal digits are the
numbers zero through seven.
• The following are all valid octal literals:
00
0777
24513600
JavaScript’s Primitive Types: Special Values
• Numeric data can take on several special values.
• When a numeric expression or variable exceeds the maximum
representable positive value, it takes on the special value Infinity.
• Likewise, when an expression or variable becomes less than the
lowest representable negative value, it takes on the value –
Infinity.
• For example, Infinity minus 100 is still Infinity; it does not become
a representable number. All Infinity values compare equal to each
other.
• Similarly, all –Infinity values compare equal.
JavaScript’s Primitive Types: Special Values
• var x = 1.7976931348623157e308; // set x to max value
• x = x + 1e292; // increment x
• alert(x); // show resulting value to user
JavaScript’s Primitive Types: Special Values
• The other important special value is NaN, which means “not a
number.”
• Numeric data takes on this value when it is the result of an
undefined operation.
• Common examples of operations that result in NaN are dividing
zero by zero, taking the sine of Infinity, and attempting to add
Infinity to –Infinity.
• The NaN value is also sticky, but unlike the infinite values it never
compares equal to anything. Because of this, you must use the
isNaN() method or compare the value to itself to determine if a
value is NaN.
• The isNaN() method returns a Boolean indicating whether the
value is NaN
JavaScript’s Primitive Types: Special Values
var x = 0 / 0; // assign NaN to x
if (x != x) // check via self-equality
{
// do something
}
if (isNaN(x)) // check via explicit call
{
// do something
}
Data Representation Issues
• If you’re working with integers, keep in mind that only
integers in the range –253 to 253 can be represented
exactly.
• As soon as your value (or an intermediate value in an
expression) falls outside of this range, its numeric
value becomes an inexact approximation. This can
lead to some surprising behavior
Data Representation Issues
var x = 9007199254740992; // 2^53
if (x == x + 1)
alert("True! Large integers are only
approximations!");
Data Representation Issues
var x = .3333;
x = x * 5;
alert(x);
String
• A string is simply text. In JavaScript, a string is a sequence
of characters surrounded by single or double quotes.
• For example,
var string1 = "This is a string";
var string2 = 'So am I';
• Unlike many other languages, JavaScript draws no
distinction between single characters and strings of
characters
String
• you can extract characters from strings using the charAt()
method.
• var myName = "Thomas";
• var thirdLetter = myName.charAt(2);
Negation
use of the – symbol besides subtraction is to negate a
value.
var x = -5;
x = -x;
// x now equals 5
Bitwise Operators
Comparison Operators
Bitwise Shift Operators
Increment and Decrement
• The ++ operator is used to increment
• For example, with
var x=3;
x++;
• the value of x is set to 4.
• Of course you could also write the increment portion of the
previous example as
x=x+1;
• Similar to the ++ operator is the –– operator, used to decrement
(subtract one from) its operand. So,
var x=3;
x--;
• leaves a value of 2 in the variable x
Post- and Pre-Increment/Decrement
• A subtle nuance of the increment (++) and decrement (– –) operators is the
position of the operator in relation to the operand.
• When the increment operator appears on the left of the operand, it is termed
a pre-increment
• while if it appears on the right, it is a post-increment.
Comparison Operators
Comparison Operators Demo
var x = 1;
var y = 5;
if (x = y)
alert("Values are the same");
else
alert("Values are different");
Comparison Operators Demo
alert(5 == "5");
Comparison Operators Demo
alert(5 === "5");
Comparing Strings
• While it is clear what comparison operators mean for numbers,
what about strings?
• For example, is the following expression true?
"thomas" >> "fritz“
1. index operator
A more common use of square brackets is the array index operator ([ ]) used to access the elements of arrays.
For example, here we define an array called
myArray:
var myArray = [2, 4, 8, 10];
To display the individual elements of the array starting from the first position (0),
we would use a series of statements like these:
alert(myArray[0]);
alert(myArray[1]);
alert(myArray[2]);
alert(myArray[3]);
New
The new operator is used to create objects. The following script creates a new instance of the
Date object and places it in the variable today.
var today = new Date();
alert(today);
The result is shown here:
Delete
In JavaScript, the delete operator is used to remove a property from an object and to remove
an element from an array.
For Example:
var myArray = ['1', '3', '78', '1767'];
document.write("myArray before delete = " + myArray);
delete myArray[2]; document.write("myArray after delete = " + myArray);
Notice that the third item, 78, has been removed from the array:
Operator Precedence and Associativity
Syllabus
• JavaScript’s Primitive Types- Numbers, Hexadecimal Literals, Octal
Literals, Special Values, Data Representation Issues, Data
Representation Issues, Strings, Undefined and Null; Composite
Types-Objects, The typeof Operator, Type Conversion, Variables.
• Operators, Expressions, and Statements- Statement Basics,
Whitespace, Termination: Semicolons and Returns, Blocks.
Operators- Assignment Operator, Arithmetic Operators, Bitwise
Operators, Bitwise Shift Operators, Increment/Decrement, Logical
Operators, void Operator, Object Operators
• Core JavaScript Statements- if Statements, switch, while Loops, do-
while Loops, for Loops, for Loops, Object-Related Statements,
Object Loops Using for… in