Java Script
Java Script
Message
It is case sensitive language.
alert('hello');
Display
name = ‘shahwaiz’;
document.write(name);
DATA TYPES
V1=3;
V2=4;
document.write(v1+v2);
alert(v1+v2);
USING HTML IN JAVA SCRIPT
&&
||
!
FUNCTION
Function myFunction()
{
document.write(‘this is function’);
}
myFunction();
JAVASCRIPT OBJECTS
<!DOCTYPE html>
<html>
<body>
Real Life Objects, Properties, <p>Creating a JavaScript Variable.</p>
and Methods <p id="demo"></p>
<script>
A car has properties like weight
var car = "Fiat";
and color, and methods like start
document.getElementById("demo").innerHTML
and stop:
= car;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object.</p>
<p id="demo"></p>
<script>
var car = {type:"Fiat", model:"500", color:"white"};
document.getElementById("demo").innerHTML = car.type;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object.</p>
<p id="demo"></p>
<script>
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
</body>
</html>
ACCESSING OBJECT PROPERTIES
p id="demo"></p>
<script>
var person = {
objectName.propertyName firstName: "John",
Or lastName : "Doe",
objectName["propertyName"]
id : 5566
};
document.getElementById("demo").innerHTML
=
person.firstName + " " + person.lastName;
</script>
ACCESSING OBJECT METHODS
<p id="demo"></p>
<script>
var person = {
firstName: "John",
lastName : "Doe",
objectName.methodName() id : 5566,
or fullName : function() {
return this.firstName + " " + this.lastName;
name = person.fullName(); }
};
document.getElementById("demo").innerHTML =
person.fullName();
</script>
JAVASCRIPT EVENTS
<script>
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = txt.length;
</script>
SPECIAL CHARACTERS
The backslash (\) escape character turns special characters into string characters:
\' ' Single quote
\" " Double quote
\\ \ Backslash
STR METHOD
• slice(start, end)
• substring(start, end)
• substr(start, length)
REPLACING STRING CONTENT
str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3Schools");
By default, the replace() function is case sensitive. Writing MICROSOFT
(with upper-case) will not work:
o replace case insensitive, use a regular expression with an /i flag
(insensitive):
str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3Schools");
To replace all matches, use a regular expression with
a /g flag (global match):
str = "Please visit Microsoft and Microsoft!";
var n = str.replace(/Microsoft/g, "W3Schools");
JAVASCRIPT NUMBERS
JavaScript has only one type of number. Numbers can be written with or without decimals.
If you add two numbers, the result will be a number:
If you add two strings, the result will be a string concatenation:
If you add a number and a string, the result will be a string concatenation:
If you add a string and a number, the result will be a string concatenation:
JAVASCRIPT MATH OBJECT
The JavaScript Math object allows you to perform mathematical tasks on
numbers.
Math.PI; // returns 3.141592653589793
Math.round(4.7); // returns 5
Math.round(4.4); // returns 4
Math.pow(8, 2); // returns 64
Math.sqrt(64); // returns 8
Math.abs(-4.7); // returns 4.7
Math.ceil(4.4); // returns 5
Math.floor(4.7); // returns 4
JAVASCRIPT MATH OBJECT
var d = new Date();
document.getElementById("demo").innerHTML = d;
• Years
• Months
• Days
• Hours
• Seconds
• Milliseconds
JAVASCRIPT GET DATE METHODS
Method Description
getFullYear() Get the year as a four digit number (yyyy)
getMonth() Get the month as a number (0-11)
getDate() Get the day as a number (1-31)
getHours() Get the hour (0-23)
getMinutes() Get the minute (0-59)
getSeconds() Get the second (0-59)
getMilliseconds() Get the millisecond (0-999)
getTime() Get the time (milliseconds since January 1, 1970)
getDay() Get the weekday as a number (0-6)
JAVASCRIPT SET DATE METHODS
Method Description
setDate() Set the day as a number (1-31)
setFullYear() Set the year (optionally month and day)
setHours() Set the hour (0-23)
setMilliseconds() Set the milliseconds (0-999)
setMinutes() Set the minutes (0-59)
setMonth() Set the month (0-11)
setSeconds() Set the seconds (0-59)
setTime() Set the time (milliseconds since January 1, 1970)
<script>
var d = new Date();
d.setFullYear(2024);
document.getElementById("demo").innerHTML = d;
</script>
JAVASCRIPT ARRAYS
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
REVERSING AN ARRAY
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Sorts the elements of fruits
fruits.reverse(); // Reverses the order of the elements
TRY IT YOURSELF (STRING)
indexOf() Returns the position of the first found occurrence of a specified value in a string
lastIndexOf() Returns the position of the last found occurrence of a specified value in a string
repeat() Returns a new string with a specified number of copies of an existing string
replace() Searches a string for a specified value, or a regular expression, and returns a new string where the specified
values are replaced
search() Searches a string for a specified value, or regular expression, and returns the position of the match
substr() Extracts the characters from a string, beginning at a specified start position, and through the specified number
of character
substring() Extracts the characters from a string, between two specified indices
Solution:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript current day and time</title>
</head>
<body></body>
</html>
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday
","Thursday","Friday","Saturday"];
document.write("Today is : " + daylist[day] + "."); if (hour===0 && prepand===' AM ')
var hour = today.getHours(); {
var minute = today.getMinutes(); if (minute===0 && second===0)
var second = today.getSeconds(); {
var prepand = (hour >= 12)? " PM ":" AM "; hour=12;
hour = (hour >= 12)? hour - 12: hour; prepand=' Midnight';
if (hour===0 && prepand===' PM ') }
{ else
if (minute===0 && second===0) {
{ hour=12;
hour=12; prepand=' AM';
prepand=' Noon'; }
} }
else Document.write("Current Time : "+hour + prepand
{ + " : " + minute + " : " + second);
hour=12;
prepand=' PM';
}
}
2. WRITE A JAVASCRIPT PROGRAM TO PRINT THE CONTENTS OF THE
CURRENT WINDOW
function print_current_page()
{
window.print();
}
2. Write a JavaScript program to print the contents of the current
window
function print_current_page()
{
window.print();
}
3. Write a JavaScript program to get the website URL (loading page)
Document.write(document.URL);
PRACTICE QUESTIONS
1. Write a JavaScript program to compare two objects to determine if the first one contains equivalent
property values to the second one
2. Write a JavaScript function to get the number of occurrences of each letter in specified string.
3. Write a JavaScript program to find duplicate values in a JavaScript array.
4. Write a JavaScript program to set the background color of a paragraph
5. Write a JavaScript function to add rows to a table.