Ganaka JavaScript
Ganaka JavaScript
com 1
JAVA SCRIPT
Key players like Netscape and Microsoft were in the midst of browser
JavaScript.
www.ganakalabs.com 2
--What Is JavaScript?
imaginative ways.
www.ganakalabs.com 3
section 1 - fundamentals of programming language
1. Introduction of js
3. keywords in js
4. identifiers
5. data types in js
6. operators
if
if else
if elseif else
switch
b) looping statements
for
while
do while
8. functions
normal function
www.ganakalabs.com 4
section 2 --- ECMA (es6)
2. Array
what
how to declare
how to use
problems
3. string
what
problems
4. functions
normal function
Anonymous function
arrow function
www.ganakalabs.com 5
OOPS - object oriented programming system
1. object
what
how to create
2. Math objects
3. Date objects
4. class
5. inheritance
6. encapsulation
7. Hoisting in JavaScript
www.ganakalabs.com 6
DOM in Java Script
www.ganakalabs.com 7
Keywords
dollar($).
The rest of the variable name can include any letter, any number, or
spaces.
www.ganakalabs.com 8
No limit to the length of the variable name.
--Operators
1) Arithmetic operator
Addition (+)
subtraction (-)
multiplication (*)
division (/)
modulus (%)
exponential (**)
increment (++)
decrement (--)
www.ganakalabs.com 9
2) assignment Operators
assigning (=)
3) Comparison Operators
www.ganakalabs.com 10
4) logical Operators
or operator (||)
Control statements are used to control the execution flow some line
Looping statements
Types :
If
If and else
1) if(condition)
body
2) if (condition)
body
else
body
www.ganakalabs.com 12
3) if (condition)
body
else if(condition)
body
else
it will reach to "else if" and check the condition and if it is true
"else if" body will executed or "else" body will execute directly
www.ganakalabs.com 13
-> Loops in JavaScript
lines repeatedly.
Types :
For
While
Do while
For in
For of
For loop : Whenever we know the start condition and end condition
Body ;
becomes false.
www.ganakalabs.com 14
While loop : Whenever we don’t know the start condition and end
Body ;
becomes false.
end condition clearly we can use the do while and it will be executed
Syntax : Do
Body ;
www.ganakalabs.com 15
The do while body is executed repeatedly , until the end condition
becomes false.
differences---
1) Initialization declaration
2) Re initialization re declaration
3) Scope
var
Re initialization is possible
www.ganakalabs.com 16
Re declaration is possible.
let
Re initialization is possible
Re declaration is Impossible.
const
Re initialization is Impossible
Re declaration is Impossible.
www.ganakalabs.com 17
JavaScript Function
JavaScript functions are defined with the function keyword.
Function Declarations
function functionName(parameters)
{
// code to be executed
}
1) Normal function
2) Anonymous function
3) Arrow function
Anonymous function
Function without the function name is nothing but
anonymous function
Statements;
www.ganakalabs.com 18
We should call the function by using variable name we
have assigned
Arrow function
Function without the function name and function
Statements;
have assigned
Array
heterogeneous
type of data.
www.ganakalabs.com 19
array values are identified by their index position, and its starts from
zero
length();
syntax: arrayname.length
concat(array);
syntax: array1.concat(array2)
toString();
The given array and it will not convert the given array into string.
syntax: array.toString();
join();
www.ganakalabs.com 20
:- join method is similar to toString method , but we can add some
pop();
:- pop method is used to delete the last element from the given
array,
and every time we call the pop method last element of current array
will be deleted.
syntax: array.pop();
push();
:- push method is used to add the new element to the last position of
the
given array.
syntax: array.push(value)
shift();
www.ganakalabs.com 21
:- shift method will delete the first element from the given array.
deleted.
syntax: array.shift()
unShift(value);
the first
syntax: array.unshift(value)
fill(value,position);
:- fill method will replace all the elements of an array with the given
value,
syntax: array.fill(value,startIndex,endIndex)
includes(value);
www.ganakalabs.com 22
:- include method will search the given element in the array, if the
element is
sort()
splice(index,how_many)
given.
slice(start_index, end_index)
:- Extracts a section of the calling array and return the elements from
indexOf()
:- Returns the first (least) index of an element within the array equal
lastIndexOf()
www.ganakalabs.com 23
:- Returns the last (greatest) index of an element within the array
equal to
.reverse()
becomes the
.forEach()
.filter()
.reduce()
.map()
ex : console.log(a.pop());
console.log(a.shift());
www.ganakalabs.com 24
Length of the new array.
ex : console.log(a.push(1));
console.log(a.unshift(1));
string methods.
1. charAt(index)
value of string.
2. substr(0, 5)
: substr method will print the characters from given index position to
3. toUpperCase()
: this method will return all the characters of given string into
uppercase format.
4. toLowerCase()
: this method will return all the characters of given string into
uppercase format.
www.ganakalabs.com 25
5. concat(str1,str2..)
6. includes("value")
7. indexOf(value)
8. lastIndexOf(value)
: last index of will give us the index position of the given character
9. replace(search_val, replace_val)
: replace method will search the given value, and if found in the
www.ganakalabs.com 26
given string it will replace that string to 2nd value.
10. trim()
: trim method will delete all the leading and trailing white spaces,
11. startsWith()
12. endsWith()
13. search()
14. split()
15. repeat()
www.ganakalabs.com 27
OOPS - object oriented programming system
object
what
1. objectName.KeyName
www.ganakalabs.com 28
2. objectName["keyname"]
or
object["key"]=new value;
delete object.key;
object.newkey = value;
www.ganakalabs.com 29
CRUD operation :- create, read, update, delete
creation, accessing,(delete,reasign,newkey),delete
this.property=property;
www.ganakalabs.com 30
Math objects
Method Description
www.ganakalabs.com 31
cbrt(x) Returns the cubic root of x
www.ganakalabs.com 32
round(x) Rounds x to the nearest integer
www.ganakalabs.com 33
Date Object
JavaScript Date Object lets us work with dates:
new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)
new Date()
new Date() creates a new date object with the current date and time:
7 numbers specify year, month, day, hour, minute, second, and millisecond
(in that order):
new Date(millisecond)
new Date(millisecond) creates a new date object with the default date and time
+ given milliseconds value.
www.ganakalabs.com 34
There are generally 3 types of JavaScript date input formats:
Type Example
www.ganakalabs.com 35
Date methods
JavaScript Get Date Methods
Method Description
www.ganakalabs.com 36
JavaScript Set Date Methods
Method Description
www.ganakalabs.com 37
JavaScript Classes
ECMAScript 2015, also known as ES6, introduced JavaScript Classes.
Syntax
class ClassName
{
constructor() { ... }
}
Class Methods
Class methods are created with the same syntax as object methods.
www.ganakalabs.com 38
JavaScript Class Inheritance
Class Inheritance
To create a class inheritance, use the extends keyword.
A class created with a class inheritance inherits all the methods from another
class:
JAVASCRIPT DOM
www.ganakalabs.com 39
getElementsByName() – select elements by
name.
getElementsByTagName() – select elements
by a tag name.
getElementsByClassName() – select
elements by one or more class names.
querySelector() – select elements by CSS
selectors.
www.ganakalabs.com 40
Get siblings of an element – get siblings
of an element.
nextSibling
nextElementSibling
previousSibling
previousElementSibling
www.ganakalabs.com 41
insertBefore() – insert a new node before
an existing node as a child node of a
specified parent node.
insertAfter() helper function – insert a
new node after an existing node as a
child node of a specified parent node.
append() – insert a node after the last
child node of a parent node.
prepend() – insert a node before the
first child node of a parent node.
replaceChild() – replace a child element
by a new element.
removeChild() – remove child elements of
a node.
www.ganakalabs.com 42
setAttribute() – set the value of a
specified attribute on a element.
getAttribute() – get the value of an
attribute on an element.
removeAttribute() – remove an attribute
from a specified element.
hasAttribute() – check if an element has
a specified attribute or not.
www.ganakalabs.com 43
Section 7. Working with Events
JavaScript events – introduce you to
the JavaScript events, the event
models, and how to handle events.
Handling events – show you three ways
to handle events in JavaScript.
1) addEventListener()
2) using onClick attribute.
3) using onclick property
Mouse events – how to handle mouse
events.
Keyboard events – how to deal with
keyboard events.
Scroll events – how to handle scroll
events effectively.
scrollIntoView – learn how to scroll an
element into view.
Focus Events – cover the focus events.
haschange event – learn how to handle the
event when URL hash changes.
www.ganakalabs.com 44
Event Delegation – is a technique of
levering event bubbling to handle events
at a higher level in the DOM than the
element on which the event originated.
dispatchEvent – learn how to generate an
event from code and trigger it.
Custom Events – define a custom
JavaScript event and attach it to an
element.
MutationObserver – monitor the DOM
changes and invoke a call back when the
changes occur.
www.ganakalabs.com 45
Select box – learn how to handle the
select box and its option in JavaScript.
Add / Remove Options – show you how to
dynamically add options to and remove
options from a select box.
Handling change event – learn how to
handle the change event of the input
text, radio button, checkbox, and select
elements.
Handling input event – handle the input
event when the value of the input element
changes
www.ganakalabs.com 46