Unit2 Javascript Notes
Unit2 Javascript Notes
Unit2 Javascript Notes
In HTML, JavaScript code must be inserted between <script> and </script> tags.
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.
Example
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
</body>
</html>
External JavaScript:
External Scripts are placed in files with extension “.js”
For example:
external.js
function test() {
document.getElementById("para1").innerHTML = "Paragraph changed.";
}
second.html
<html>
<head>
<script src="external.js">
</script>
</head>
<body>
</body>
</html>
JavaScript Comments
JavaScript comments can be used to explain JavaScript code, and to make it more readable.
JavaScript comments can also be used to prevent execution, when testing alternative code.
Any text between // and the end of the line will be ignored by JavaScript (will not be executed).
// Change heading:
document.getElementById("myH").innerHTML = "My First Page";
// Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";
Multi-line Comments
Multi-line comments start with /* and end with */.
This example uses a multi-line comment (a comment block) to explain the code:
Example
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:
*/
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";
JavaScript Variables
JavaScript variables are containers for storing data values.
Example
var x = 5;
var y = 6;
var z = x + y;
The general rules for constructing names for variables (unique identifiers) are:
JavaScript Operators
The assignment operator (=) assigns a value to a variable.
Assignment
var x = 10;
Adding
var x = 5;
var y = 2;
var z = x + y;
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
++ Increment
-- Decrement
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
Operator Description
== equal to
!= not equal
!== not equal value or not equal type
? ternary operator
Operator Description
|| logical or
! logical not
The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Syntax
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
}
Example
If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00,
create a "Good day" greeting, otherwise a "Good evening":
<html>
<body>
<p id="demo"></p>
<script>
function myFunction() {
var greeting;
} else {
document.getElementById("demo").innerHTML = greeting;
</script>
</body>
</html>
JavaScript Switch Statement
The switch statement is used to perform different actions based on different conditions.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Example:
<html>
<body>
<p id="demo"></p>
<script>
var day;
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
document.getElementById("demo").innerHTML = "Today is " + day;
</script>
</body>
</html>
JavaScript Loop
Loops can execute a block of code a number of times. If you want to run the same code over
and over again, each time with a different value.
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example
<html>
<body>
<h2>JavaScript Loops</h2>
<p id="demo"></p>
<script>
var text = "";
var n=17;
var i;
for (i = 1; i <= 10; i++) {
text += n+" * "+i+" = "+n*i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Example
<html>
<body>
<h2>JavaScript Loops</h2>
<p>The for/in statement loops through the properties of an object.</p>
<p id="demo"></p>
<script>
var txt = "";
var person = {fname:"John", lname:"Doe", age:25, bl_gr:"A+"};
var x;
for (x in person) {
txt += person[x] + "<br />";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
Syntax
while (condition) {
// code block to be executed
}
Example:
<html>
<body>
<h2>JavaScript while</h2>
<p>Enter a number:<input type="text" id="numb" /></p>
<button onclick="fact()">Factorial??</button>
<p id="demo"></p>
<script>
function fact(){
var text = "";
var n=document.getElementById("numb").value;
var i = 1;
var f=1;
while (i <= n) {
f*=i;
i++;
}
alert("factorial of "+n+" is "+f)
}
</script>
</body>
</html>
Syntax
do {
// code block to be executed
}
while (condition);
Example:
<html>
<body>
<h2>JavaScript do ... while</h2>
<p id="demo"></p>
<script>
var text = ""
var i = 1;
do {
text += "<br>The number is " + i;
i++;
}
while (i <= 10);
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
The break statement breaks the loop and continues executing the code after the loop (if any):
Example
<html>
<body>
<p id="demo"></p>
<script>
var text = "";
var i;
for (i = 1; i <= 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
<html>
<body>
<p id="demo"></p>
<script>
var text = "";
var i;
for (i = 1; i <= 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task.
Example
function myFunction(p1, p2) {
return p1 * p2; // The function returns the product of p1 and p2
}
A JavaScript function is defined with the function keyword, followed by a name, followed by
parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules as
variables).
Example:
<html><head><script>
//if you will pass 77, you will get 25
function toCelsius(f) {
document.getElementById("demo").innerHTML = (5/9) * (f-32);
}
</script></head>
<body>
<h2>JavaScript Functions</h2>
<p>Value in Fahrenite:<input type="text" id="tempFahr" /></p>
<button onclick="toCelsius(document.getElementById('tempFahr').value)">fahr to
cels</button>
<p id="demo"></p>
</body>
</html>