WT Unit II JavaScript Basics
WT Unit II JavaScript Basics
UNIT 2
JavaScript -
Basics
JavaScript - 1/
Web Technologies (9FC06) -
UNIT 2
Unit - 2 :
Content
JavaScript Introduction
Overview of
JavaScript Variables
Data Types
Functions
Arrays
Objects in
JavaScript
:
Boolean,
Number,
String,
Date,
Math
Regular JavaScript - 2/
Web Technologies (9FC06) - JS
UNIT 2 Introduction
JavaScript Introduction
Overview of JavaScript
JavaScript is a high-level, interpreted scripting language
primarily used for client-side web development.
It was introduced in 1995 by Netscape and has since
become one of the most widely used programming
languages.
JavaScript is known for its flexibility, as it allows
developers to create
dynamic and interactive web applications.
Importance in Web Development
JavaScript plays a crucial role in modern web development,
enabling the creation of dynamic user interfaces and
responsive web pages.
It is used for tasks such as form validation, DOM
manipulation, event handling, and asynchronous
programming.
With the advent of frameworks like React, Angular, and
JavaScript - 3/
Web Technologies (9FC06) - JS
UNIT 2 Introduction
Features of JavaScript
Dynamic Typing
JavaScript is dynamically typed, meaning variable types are
determined at runtime rather than compile time.
This allows for flexibility but can also lead to unexpected
behavior if types are not properly managed.
Prototype-based Inheritance
In JavaScript, objects inherit properties and methods
directly from other objects through prototypes.
This differs from class-based inheritance in languages
like Java or C++, providing a more flexible object
model.
First-class Functions
Functions in JavaScript are treated as first-class citizens,
meaning they can be assigned to variables, passed as
arguments, and returned from other functions.
This functional programming paradigm enables powerful
techniques like higher-order functions and closures.
JavaScript - 4/
Web Technologies (9FC06) - JS
UNIT 2 Introduction
<script >
/ / I n l i n e J a v a S c r i p t code
d o c u m e n t . w r i t e ( " H e l l o , World ! " ) ;
</ s c r i p t >
// s c r i p t . j s
f u n c ti o n d i s p l a y D a t e ( )
{ document. getElementBy I d ( "
demo " ) . innerHTML = Date ( ) ;
} JavaScript - 8 / 49
Web Technologies (9FC06) - JS
UNIT 2 Variables
JavaScript Variables I
Declaration and Initialization
JavaScript variables are declared using the var, l e t , or const
keywords.
The var keyword was traditionally used for variable
declaration, but l e t and const introduced in ES6 provide
block-scoping and immutability, respectively.
Example:
var x = 5 ;
let y = ’Hello’;
const P I = 3 . 1 4 ;
Data Types
JavaScript variables can hold various data types, including
numbers, strings, booleans, objects, arrays, functions, and
symbols.
Unlike many other programming languages, JavaScript is
dynamically typed, meaning the type of a variable is
inferred at runtime.
JavaScript - 9 / 49
Web Technologies (9FC06) - JS
UNIT 2 Variables
JavaScript
Variables II
Variable Scope
Variables declared with var are function-scoped, whereas
variables declared with l e t and const are block-scoped.
Block-scoped variables are only accessible within the block
they are defined in, providing better control over variable
scope and reducing the risk of unintended side effects.
Hoisting
In JavaScript, variable declarations are hoisted to the
top of their containing scope during compilation.
However, only the declaration is hoisted, not the
initialization. This means that variables declared with var are
initialized with undefined until their value is assigned.
JavaScript - 10 /
Web Technologies (9FC06) - UNIT 2 JS
Variables
Data Types
JavaScript variables can hold various data types, including
numbers, strings, booleans, objects, arrays, functions, and
symbols.
Unlike many other programming languages, JavaScript is
dynamically typed, meaning the type of a variable is
inferred at runtime.
We can know
< ! DOCTYPE the
html > type o f a va r i a b l e using typeof Operator
Example:
<html >
<head >
< ti t l e > H e l l o , World ! < / ti t l e >
</ head >
<body >
<h1 i d = " v t y p e " > </ h1 >
< s c r i p t type =" t e x t / j a v a s c r i p t "
> var a=4;
c o n s o l e . l o g ( t y p e o f ( a ) ) ; / / number
var b =5.6;
JavaScript - 12 /
Web Technologies (9FC06) - JS
UNIT 2 Variables
Data
Types
c o n s o l e . l o g ( t y p e o f ( b ) ) ; / / number
var c = ’ SNIST ’ ;
console . l o g ( typeof( c ) ) ; / /
s t r i n g v a r d= t r u e ;
c o n s o l e . l o g ( t y p e o f ( d ) ) ; / / number
</ s c r i p t >
</ body >
</ html >
Output:
JavaScript - 13 /
Web Technologies (9FC06) - UNIT 2 JS
Variables
2 Function Expression:
Defined using a variable assignment where the value is a
function. Cannot be invoked before they are defined.
Example:
JavaScript - 15 /
Web Technologies (9FC06) - UNIT 2 JS
Variables
JavaScript - 16 /
Web Technologies (9FC06) - UNIT 2 JS
Variables
JavaScript - 17 /
Web Technologies (9FC06) - UNIT 2 JS
Variables
l e t car={
name : " TATA "
}
f u n c ti o n d i s p l a y c a r ( c o l o r , p r i c e ) {
c o n s o l e . l o g ( " c a r name i s " + t h i s . name + " , C o l o r i s "
+ c o l o r + " and P r i c e i s " + p r i c e )
}
d i s p l a y c a r . c a l l ( c a r , " Red " , 1 2 0 0 0 0 0 ) ;
d i s p l a y c a r . a p p l y ( c a r , [ " Green " , 2 4 0 0 0 0 0 ] ) ;
v a r n e wca r = d i s p l a y c a r . b i n d ( c a r , " White " , 1 0 0 0 0 0 0 ) ;
newcar ( ) ;
co nso le . l o g ( d i s p l a y c a r. to S t r i n g ( ) ) ;
JavaScript - 18 /
Web Technologies (9FC06) - JS
UNIT 2 Variables
Objects in JavaScript:
Array
Example:r e(Continued...)
turn f r u i t [0]== " a";
});
co n s o l e . l o g ( " Ever y : " + sta r tsw it h A) ;
v a r x= f r u i t s . fi l t e r ( f u n c ti o n ( f r u i t ) {
return f r u i t [0]== " a";
});
console . l o g ( " F i l t e r : " + x ) ;
v a r x1 = f r u i t s . fi n d ( f u n c ti o n ( f r u i t )
{ return f r u i t [0]== " a";
});
c o n s o l e . l o g ( " F i n d : " +x1 ) ;
v a r x1 = f r u i t s . fi n d I n d e x ( f u n c ti o n ( f r u i t )
{ return f r u i t [0]== " a";
});
c o n s o l e . l o g ( " F i n d I n d e x " +x1 ) ;
JavaScript - 21 /
Web Technologies (9FC06) - JS
UNIT 2 Variables
Objects in JavaScript:
Array
f r u i t s(Continued...)
Example: . copyWithin (2 , 1 , 3 ) ;
c o n s o l e . l o g ( " a ft e r co py : " + f r u i t s ) ;
f r u i t s . fi l l ( 2 ) ;
console . l o g ( f r u i t s ) ;
</ s c r i p t >
</ body >
</ html >
Output:
JavaScript - 22 /
Web Technologies (9FC06) - UNIT 2 JS
Variables
Example
l e t f r u i t s = [ " A p p l e " , " Banana " , " Orange " ] ;
f r u i t s . push ( " Mango " ) ;
c o n s o l e . l o g ( f r u i t s ) ; / / O u t p u t : [ " A p p l e " , " Banana
" , " Orange " , " Mango " ]
console . l o g ( f r u i t s . j o i n ( " , " ) ) ; // Output: "
Apple , Banana , Orange , Mango "
JavaScript - 23 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Objects in JavaScript
Introduction
Objects are one of the most fundamental concepts in
JavaScript.
They are used to store collections of data and methods that
operate on that data.
Objects in JavaScript are dynamic, meaning they can be
modified at
runtime by adding or removing properties and methods.
Prototypes and Prototypal Inheritance
In JavaScript, objects can inherit properties and methods
from other objects through prototypes.
Every JavaScript object has a prototype chain, which
allows it to inherit properties and methods from its
prototype object.
Prototypal inheritance is a key feature of JavaScript
that allows for
JavaScript - 24 /
Web Technologies (9FC06) - UNIT 2 JS
Objects
JavaScript - 25 /
Web Technologies (9FC06) - UNIT 2 JS
Objects
Overview
The Boolean object represents one of two values: true or
false. It is used for logical operations and conditional
expressions.
The Boolean value of 0, -0, ””(empty string), undefined,
null, false, NaN is false.
Method Description
valueOf() Returns the primitive value of the Boolean
toString() object.
Returns a string representation of the Boolean
object.
Example
l e t b o o l O b j = new B o o l e a n ( t r u e ) ;
c o n s o l e . l o g ( b o o l O b j . v a l u e Of ( ) ) ; / / O u t p u t : t r u e
co nso le . l o g ( b o o l O b j . to S t r i n g ( ) ) ; / / O utput: " true
"
JavaScript - 27 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Overview
The Number object represents numerical values in
JavaScript.
It can represent integers, floating-point numbers, and
special numeric values like NaN and I n fi n i t y .
Method Description
valueOf() Returns the primitive value of the Number
toString() object.
to F i xe d ( ) Returns a string representation of the Number
object. Returns a string representing the
Number object using fixed-point notation.
Example
l e t num O bj = new Number ( 3 . 1 4 1 5 9 ) ;
c o n s o l e . l o g ( num O b j . t o F i x e d ( 2 ) ) ; / / O u t p u t : " 3 . 1 4 "
JavaScript - 28 /
Web Technologies (9FC06) - JS Objects
UNIT 2
vdaorc u m
i nednetx. =wsr ti tr e. l( a’ s<t bI nr /d >e ’x)O; f ( "
W"); JavaScript - 31 /
Web Technologies (9FC06) - UNIT 2 JS
Objects
va r newstr= s t r . s l i c e ( 1 , 6 ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
document. w r i t e ( " S l i c e d S t r i n g : " + n e w s t r ) ;
va r newstr= s t r . s u b s t r ( 1 , 2 ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
document. w r i t e ( " s u b s t r : " + n e w s t r ) ;
va r newstr= s t r . s u b s t r i n g ( 1 , 6 ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
document. w r i t e ( " s u b s t r i n g : " + n e w s t r ) ;
va r newstr= s t r . to UpperCase ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
document. w r i t e ( " Uppercase : " + n e w s t r ) ;
va r newstr= s t r . to LowerCase ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
document. w r i t e ( " Lowercase : " + n e w s t r ) ;
JavaScript - 33 /
Web Technologies (9FC06) - UNIT 2 JS
Objects
JavaScript - 34 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Objects in JavaScript:
Date
< ! DOCTYPE html >
Example:
< h t m l l a n g = " en" >
<head >
<meta c h a r s e t = " UTF - 8 " >
<meta name = " v i e w p o r t " c o n t e n t = " w i d t h = d e v i c e - w i d t h ,
i n i ti a l - s c a l e = 1 . 0 " >
< ti t l e > Document < / ti t l e >
</ head >
<body >
< s c r i p t type =" t e x t / j a v a s c r i p t "
> v a r d= new Date ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
d o c u m e n t . w r i t e ( " C u r r e n t Date :
" +d);
var x = d . getDate ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
d o c u m e n t . w r i t e ( " C u r r e n t Day : " + x ) ;
v a r x = d . g e t Month ( ) JavaScript
; - 36 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Objects in JavaScript:
Date
Example:(Continued...)
v a r x = d . getMonth ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
d o c u m e n t . w r i t e ( " C u r r e n t Month : " +( x + 1 ) ) ;
var x = d . g e t F u l l Ye a r ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
document. w r i t e ( " C u r re n t F u l l Ye a r : " + x ) ;
var x = d . getHours ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
d o c u m e n t . w r i t e ( " H o urs : " + x ) ;
var x = d . getMinutes ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
d o c u m e n t . w r i t e ( " M i n u te s : " +x);
JavaScript - 37 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Objects in JavaScript:
Date
Example:(Continued...)
var x=d. getSeconds ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
document. w r i t e ( " Seconds : " +x);
</ s c r i p t >
</ body >
</ html >
Output:
JavaScript - 38 /
Web Technologies (9FC06) - UNIT 2 JS
Objects
v a r x = d . g e t U TC H o u r s ( ) ;
document. w r i t e ( ’ < b r / > ’ ) ;
d o c u m e n t . w r i t e ( " H o urs : " + x ) ; / /
7
JavaScript - 39 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Overview
The Math object provides mathematical constants and
functions in JavaScript.
It is used for performing mathematical operations such as
trigonometry, logarithms, and exponentiation.
Method Description
a bs ( ) Returns the absolute value of a number.
a co s ( ) Returns the arccosine of a number.
acosh() Returns the hyperbolic arccosine of a number.
asin() Returns the arcsine of a number.
asinh() Returns the hyperbolic arcsine of a number.
ata n ( ) Returns the arctangent of a number.
atan2() Returns the arctangent of the quotient of its
arguments.
atanh() Returns the hyperbolic arctangent of a number.
cbrt() Returns the cube root of a number.
JavaScript - 40 /
Web Technologies (9FC06) - UNIT 2 JS
Objects
Example
console . l o g ( Math . a b s ( - 5 ) ) ; / / O u t p u t : 5
console . l o g ( Math . s q r t ( 2 5 ) ) ; / / O u t p u t : 5
console . l o g ( Math . s i n ( Math . P I / 2 ) ) ; / / O u t p u t : 1
(
sine o f 90 d e g r e e s )
JavaScript - 41 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Regular Expression
Overview
Regular expressions (regex) are patterns used to match
character combinations in strings.
They are used for searching, replacing, and validating
strings based on
patterns.
Method Description
exec() The exec() methods searches a string for a
specified
value and returns the first match. or else
returns null
test() The test() method tests for a match in a
string and
returns true or false.
match() The match() method retrieves the matches of a
pattern
in a string. It returns an array of matches or
null if no match is found.
search () The search() method searches for a pattern in a
string JavaScript - 42 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Regular
Expression
Method Description
re p l a c e ( ) The replace() method searches for a pattern in a
string
and replaces it with a replacement string.
split() The split() method splits a string into an array
of sub-
strings using the pattern as a delimiter.
JavaScript - 43 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Regular
Expression
< ! DOCTYPE html >
Example:
< h t m l l a n g = " en" >
<head >
<meta c h a r s e t = " UTF - 8 " >
<meta name = " v i e w p o r t " c o n t e n t = " w i d t h = d e v i c e - w i d t h ,
i n i ti a l - s c a l e = 1 . 0 " >
< ti t l e > Document < / ti t l e >
</ head >
<body >
< s c r i p t type =" t e x t / j a v a s c r i p t " >
/ / 1 . i : c a s e - i n s e n s i ti v e
v a r t e x t = " Welcome t o my w o r l d o r y o u r World " ;
v a r p a tt e r n = / welcome / i ;
v a r r e s u l t = t e x t . match ( p a tt e r n ) ;
c o n s o l e . l o g ( r e s u l t ) ; / / Welcome
/ / e m a i l p a tt e r n s e a r c h
v a r e m a i l P a tt e r n = / ^ [ ^ \ s@ ]+@
v[ ^a \r s@
em] +a \i l.=[ "^ \v is@
j a y] +k u$m
/ ;a r. b@- s r e e n i d h i . edu . i n " ;
JavaScript 44 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Regular Expression
Example: (Continued..)
v a r e m a i l = " v i j a y k u m a r. b@ s r e e n i d h i . edu . i n " ;
v a r r e s u l t = e m a i l . s e a r c h ( e m a i l P a tt e r n ) ;
console . l o g ( r e s u l t ) ;
/ / e m a i l p a tt e r n t e s t
v a r e m a i l P a tt e r n 1 = / ^ [ ^ \ s@ ]+@ [ ^ \ s@ ] + \ . [ ^ \ s@ ] +
$ / ; v a r e m a i l = " v i j a y k u m a r. b@ s r e e n i d h i . edu . i n " ;
v a r r e s u l t = e m a i l P a tt e r n 1 . t e s t ( e m a i l ) ;
console . l o g ( r e s u l t ) ;
/ / e m a i l p a tt e r n exec
v a r e m a i l P a tt e r n 2 = / ^ [ ^ \ s@ ]+@ [ ^ \ s@ ] + \ . [ ^ \ s@ ] +
$ / ; v a r e m a i l = " v i j a y k u m a r. b s r e e n i d h i . edu . i n " ;
v a r r e s u l t = e m a i l P a tt e r n 2 . e x e c ( e m a i l ) ;
console . l o g ( r e s u l t ) ;
v a r p a tt e r n = / a p p l e s / ;
v a r s t r = " I have 2 a p p l e s . " ;
JavaScript - 45 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Regular
Expression
Example:
v a r r e(Continued..)
s u l t = s t r . r e p l a c e ( p a tt e r n , " o r a n g e s " ) ;
console . l o g ( r e s u l t ) ; // " I have 2 o r a n g e s . "
Output:
JavaScript - 46 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Questio
ns
Short Answer
Questions
1 Explain the role of JavaScript in web development.
JavaScript - 47 /
Web Technologies (9FC06) - JS Objects
UNIT 2
Questions
Long Answer
Questions
1 Discuss the evolution of JavaScript from its inception to its
Thank
You
JavaScript - 49 /