Documente Academic
Documente Profesional
Documente Cultură
JAVASCRIPT
CDE TEAM EVENTS
INTRODUCTION TO JAVASCRIPT
Getting Started !
CDE TEAM EVENTS
INTRODUCTION TO JAVASCRIPT
▸ Invented by Brendan Eich at NetScape
▸ Then LiveScript
▸ Then JavaScript
JAVASCRIPT MISCONCEPTION
▸ JavaScript is not Java
WHAT IS JAVASCRIPT ?
▸ JavaScript is a Programming Language
JavaScript in HTML
CDE TEAM EVENTS
WHAT IS JAVASCRIPT ?
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
CDE TEAM EVENTS
HELLO JAVASCRIPT !
JavaScript Basics
CDE TEAM EVENTS
IDENTIFIERS
▸ An Identifier is the name given to a variable,
function, property or function argument.
STATEMENTS
▸ Statements in JavaScript are terminated by a semicolon.
But not must.
COMMENTS
▸ JavaScript uses C-style comments for both single-line
and multi-line comments.
▸ /*
* This is multi line
* comments
*/
CDE TEAM EVENTS
VARIABLES
▸ JS variables are loosely typed, meaning that a variable
can hold any type of data.
OPERATORS
▸ Identically Equal (===) and Not Identically (!==) Equal
operators do the same things as equal and not equal,
except that they do not convert operands before testing
the equality.
▸ Ex:
var result1 = ( “55” == 55 ); // returns true
var result2 = ( “66” === 66 ); // returns false
CDE TEAM EVENTS
DATA TYPES
▸ There are 5 simple(Primitive) data types :
1. Undefined
2. Null
3. Boolean
4. Number
5. String
UNDEFINED
▸ The Undefined type has only one value, which the
special value of undefined.
NULL
▸ The Null type has only one value, which the special value
of null.
BOOLEAN
▸ The Boolean type has only two literal values, true and
false.
NUMBER
▸ The most interesting data type in JavaScript is Number, which
uses IEEE-754 format to represent both integer and floating-
point values.
STRING
▸ A String data type represents a sequence of zero or more
16-bit unicode characters.
▸ Ex:
var firstName = “Sasidhar”;
var lastName = ‘Kola’;
CDE TEAM EVENTS
TYPEOF OPERATOR
▸ Because JS is loosely typed, there needs to be a way to
determine the data type of a give variable.
SAYHELLO ( “JAVASCRIPT “ );
JavaScript Functions
CDE TEAM EVENTS
FUNCTIONS
▸ Functions are the core of any programming language.
NO OVERLOADING
▸ JavaScript functions can’t be overloaded in the traditional sense like
in other Programming Languages.
▸ Ex:
function addNumber(num){
return num + 100;
}
function addNumber(num){
return num + 200;
}
addNumber(100); // prints 300
CDE TEAM EVENTS
ARRAYS
▸ An Array in JS is very different from arrays in other programming languages.
▸ Unlike other languages, arrays in JS holds any type of data in each slot.
▸ ex:
var colours = new Array ( ) ;
var colours = new Array ( 20 ) ;
var colours = new Array ( “red”, “green”, “blue” ) ;
var colours = [ ] ;
var colours = [ “red”, “green”, “blue” ];
CDE TEAM EVENTS
OBJECTS
▸ Objects in JavaScript start out as nonspecific groups of
data and functionality.
▸ Ex:
var user = new Object ( );
user.name = “Sasidhar”; // assign property
document.write(user.name); // access property
CDE TEAM EVENTS
▸ Ex:
var user = { first_name: ‘Sasidhar’ };
user.last_name = “Sasidhar”; // assign property
document.write(user.first_name + user.last_name);
// access property
CDE TEAM EVENTS
STRING.REVERSE( )
String Methods
CDE TEAM EVENTS
STRING METHODS
▸ var name = “Aditya”;
▸ name.toUpperCase() | name.toLowerCase();
CDE TEAM EVENTS
DATE.NOW ( )
Date Methods
CDE TEAM EVENTS
DATE
▸ var date = new Date ( );
▸ document.write(date);
▸ document.write(date.getDate());
▸ document.write(date.getMonth());
▸ document.write(date.getFullYear());
▸ document.write(date.getTime());
▸ document.write(date.getHours());
▸ document.write(date.getMinutes());
CDE TEAM EVENTS
MATH.RANDOM( )
Math Methods
CDE TEAM EVENTS
MATH
▸ var random = Math.random( );
STRING ( 10 );
Type Conversion
CDE TEAM EVENTS
TYPE CONVERSION
▸ JavaScript variables can be converted to a new variable and
another data type by using
1. A JavaScript function
2. Automatically by JS itself
▸ Eg:
String ( 20 );
Boolean ( 20 );
CDE TEAM EVENTS
CONVERT TO STRING
▸ String ( 10 ); | 20.toString ( );
▸ 3.14159.toFixed( 2 );
CONVERT TO NUMBER
▸ Number ( “24.8” );
▸ Number ( “ “ ) ; // prints 0
▸ parseInt ( ); | parseFloat ( );
▸ var a = “10”;
var b = + a; // b is a number
CDE TEAM EVENTS
CONVERT TO NUMBER
▸ Number ( “24.8” );
▸ Number ( “ “ ) ; // prints 0
▸ parseInt ( ); | parseFloat ( );
▸ var a = “10”;
var b = + y; // b is a number
▸ Number ( false ) // 0
▸ Number ( null ) // 0
CDE TEAM EVENTS
CONVERT TO BOOLEAN
▸ Boolean ( “Aditya” ); // true
▸ Boolean ( ““ ); // false
▸ Boolean ( 1 ); // true
▸ Boolean ( 0 ); // false
Scope
CDE TEAM EVENTS
SCOPE
▸ In JS there are two types of scopes
1. Local
2. Global
HELLO DOM !
WHAT IS DOM ?
▸ Whenever a web page loads into the browser, it creates
Document Object Modal of that page.
HTML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
CDE TEAM EVENTS
DOM REPRESENTATION
CDE TEAM EVENTS
POWER UP JAVASCRIPT
▸ With the object model, JS gets all the power it needs to create
dynamic HTML.
▸ JS can add, modify and remove all HTML elements & attributes.
▸ e.g.:
document.getElementById( “test” ).innerHTML = “Hello
DOM” ;
CDE TEAM EVENTS
DOCUMENT OBJECT
▸ document object is the owner of all other objects in web
page.
▸ e.g.:
document.write ( “Hello DOM” );
CDE TEAM EVENTS
▸ document.getElementById(“element_id”);
▸ document.getElementsByClassName(“red”);
▸ document.getElementsByTagName(“p”);
CDE TEAM EVENTS
▸ Syntax:
document.getElementById(id).innerHTML = new HTML;
CDE TEAM EVENTS
MODIFYING CSS
▸ To change the style of an HTML element, use this syntax:
document.getElementById(id).style.property = new style
CDE TEAM EVENTS
▸ e.g.:
1. Mouse Clicked
2. Key Pressed
▸ Syntax:
<element event=“js_function()”>
CDE TEAM EVENTS
▸ You can add many event handlers of the same type to one element, i.e two
"click" events.
▸ Syntax:
element.addEventListener ( event, function, useCapture );
CDE TEAM EVENTS
FORMS
▸ document.forms returns all the available forms in your
HTML page as a collection.
element.
▸ Steps to be followed:
1. Create element by using document.createElement(element);
element.
▸ Steps to be followed:
1. Create element by using document.createElement(element);
DOM TRAVERSING
▸ With the HTML DOM, you can navigate the node tree using
node relationships.
▸ Node Relationships:
1. In a node tree, the top node is called the root (or root node).
2. Every node has exactly one parent, except the root (which has
no parent).
1. parentNode
3. firstChild
4. lastChild
5. nextSibling
6. previousSibling
CDE TEAM EVENTS
HELLO BOM !
WINDOW OPERATION
▸ window.open ( ‘url’ ); // opens a new window
SCREEN
▸ The window.screen object contains information about
the user's screen.
▸ screen.width ▸ screen.availHeight
▸ screen.height ▸ screen.colorDepth
▸ screen.availWidth ▸ screen.pixelDepth
CDE TEAM EVENTS
LOCATION
▸ The window.location object can be used to get the
current page address (URL) and to redirect the browser
to a new page.
▸ location.href
▸ location.hostname
▸ location. pathname
▸ location. protocol
CDE TEAM EVENTS
HISTORY
▸ The window.history object contains the browsers history.
POPUPS
▸ JavaScript has three kind of popup boxes:
1. Alert box
2.Confirm box
3. Prompt box.
CDE TEAM EVENTS
TIMING FUNCTIONS
▸ setTimeout(function, milliseconds) ;
▸ setInterval(function, milliseconds);
ES 6
CDE TEAM EVENTS
CONSTANTS
▸ ES6 Support for constants (also known as "immutable
variables”).
▸ Syntax:
const constant_name = value;
▸ e.g.:
const PI = 3.141593;
CDE TEAM EVENTS
SCOPING
▸ ES6 Support for block-level scope.
▸ Syntax:
let variable_name = value;
▸ e.g.:
let name = “Aditya”;
CDE TEAM EVENTS
ARROW FUNCTIONS
▸ ES6 Support for arrow functions.
▸ Syntax:
let variable_name = ( arg1, arg2 ) => {
// function definition
};
▸ e.g.:
let add = ( num1, num2) => {
return num1 + num2;
}
CDE TEAM EVENTS
TEMPLATE LITERALS
▸ ES6 Support for template literals (also known as
Template Strings).
▸ e.g:
var message = `Hello, this ${ name} from ${college}`;
CDE TEAM EVENTS
CLASSES
ES 6 CONT.
CDE TEAM EVENTS
CLASSES
▸ ES6 Support for Classes.
▸ Syntax:
class ClassName {
// class definition
}
▸ e.g.:
class Person {
}
CDE TEAM EVENTS
▸ Syntax:
class Student extends User {
// class definition
}
CDE TEAM EVENTS
▸ Syntax:
class ClassName {
static classMemberName = () => {
}
}
CDE TEAM EVENTS
▸ Syntax:
class ClassName {
get getterName() { }
set setterName() {}
}
CDE TEAM EVENTS
ES 6 CONT.
CDE TEAM EVENTS
SETS
▸ SETs are cleaner data structures to hold unique values
MAPS
▸ MAPs are cleaner data structures to hold unique values