Javascript
Javascript
DEFINITION
<button type="button"
onclick="document.getElementById('demo_id').inner
HTML = Date()">
Click me to display Date and Time.</button>
<h1 id="demo_id"></h1>
</body>
</html>
• JavaScript Can Change HTML Content.
Ex: document.getElementById("demo").innerHTML = "Hello
JavaScript";
JAVASCRIPT CAN CHANGE THE STYLE OF AN HTML ELEMENT.
<SCRIPT> Tag
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
JAVASCRIPT FUNCTIONS AND EVENTS
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
</html>
EXTERNAL JAVASCRIPT
<script src="myScript.js"></script>
JAVASCRIPT OUTPUT
w r i win
t.
m en dow
oc u () .ale
d te rt()
co
L
TM
ns
ole
H
er
.lo
inn
g(
)
<script>
document.getElementById("demo").innerHTML = 2 + 2;
</script>
ML
HT
er
inn
<script>
r i document.write(2 + 2);
t. w
m en </script>
oc u ()
d te
<button type="button" onclick="document.write(“Welcome” )">Try it</button>
Welcome
win
dow
.ale
rt()
<script>
window.alert(5 + 6);
</script>
co
<!DOCTYPE html>
ns
<html>
ole
<body>
.lo
<h2>Activate debugging with F12</h2>
g(
)
<script>
console.log(3 + 2);
</script>
</body>
</html>
JAVASCRIPT STATEMENTS
• function myFunction() {
document.getElementById("demo1").innerHTML = "Hello world!";
document.getElementById("demo2").innerHTML = "How are you?";
}
Keyword Description
break Terminates a switch or a loop
S
continue Jumps out of a loop and starts at the top
D
debugger Stops the execution of JavaScript, and calls (if
available) the debugging function
OR
do ... while Executes a block of statements, and repeats
the block, while a condition is true
YW
for Marks a block of statements to be executed,
as long as a condition is true
function Declares a function
if ... else
KE Marks a block of statements to be executed,
depending on a condition
return Exits a function
JS
• var x;
x = 6;
JAVASCRIPT OPERATORS
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement
JAVASCRIPT STRING OPERATORS
• var txt1 = ”New";
var txt2 = ”Delhi";
var txt3 = txt1 + " " + txt2;
• var x = 5 + 5;
var y = "5" + 5;
var z = "Hello" + 5;
ADDING STRINGS AND NUMBERS
• var x = 5 + 5;
var y = "5" + 5;
var z = "Hello" + 5;
• Output:
• 10
55
Hello5
JAVASCRIPT COMPARISON OPERATORS
Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
JAVASCRIPT LOGICAL OPERATORS
Opera Description
tor
&& logical and
|| logical or
! logical not
JAVASCRIPT BITWISE OPERATORS
• var x = 2;
var y = 2;
var z = 6;
•
(x == y) // Returns true
(x == z) // Returns false
JAVASCRIPT ARRAYS
• var sringArray = [“a", “b", “c"];
JAVASCRIPT OBJECTS
<h2>JavaScript Objects</h2>
<p id="demo"></p>
<script>
var person = {
firstName : "Dinesh",
lastName : "Kumar"};
document.getElementById("demo").innerHTML =
person.firstName;
</script>
</body>
</html>
DIFFERENCE BETWEEN UNDEFINED AND
NULL
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true
JAVASCRIPT FUNCTIONS
<script>
function myFunction (p1, p2) {
return p1 * p2;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>
SYNTAX
• function name(parameter1, parameter2,
parameter3) {
// code to be executed
}
WHAT IS THIS?
var person = {
firstName: "John",
lastName : "Doe”,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
John Doe
HTML EVENTS
• Length
• var txt = “ABC";
var sln = txt.length;
INDEXOF()
index of (the position of) the first occurrence of a specified text in a string
var str = “Welcome to Javascript;
var pos = str.indexOf(“to");
INDEXOF()
index of (the position of) the first occurrence of a specified text in a string
var str = “Welcome to Javascript;
var pos = str.indexOf(“to");
=8
concat() joins two or more strings:
• if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
JAVASCRIPT SWITCH STATEMENT
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
JAVASCRIPT FOR LOOP
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
for (i = 0; i < 5; i++) {
alert(i);
}
THE WHILE LOOP
while (condition) {
// code block to be executed
}
while (i < 10) {
text += “The text=" + i;
i++;
}
<script>
var text = "";
var i = 0;
while (i < 10) {
text += "<br>The number is " + i;
i++;
}
document.getElementById("demo").innerHTML =
text;
</script>
Thank You