Lecture 5- Java Script
Lecture 5- Java Script
5
JavaScript is a cross-platform, object-
oriented scripting language. It is a small
and lightweight language.
<script>
document.getElementById("demo").i
nnerHTM L = "My First JavaScript";
</script>
A JavaScript function is a block of
JavaScript code, that can be executed
when the function is called or invoked.
For example, a function can be executed
when an event occurs, like when the
user clicks a button
</body>
</html>
<html>
<body>
<script>
document.write(5 +
6);
</script>
</body>
<html>
<body>
<script>
document.getElementById("demo").inne
rHTML =
5 + 6;
</script>
</body>
</html>
<html>
<body>
</body>
</html>
JavaScript syntax is the set of rules,
how JavaScript programs are
constructed.
JavaScript Programs
1. A computer program is a list of
"instructions" to be "executed" by the
computer. JavaScript statements are
composed of: Values, Operators,
Expressions, Keywords, and Comments.
2. In a programming language, these
program instructions are called
statements.
3. JavaScript is a programming
language.
<body>
<h1>JavaScript Statements</h1>
<p>Statements are separated by semicolons.</p>
<p>The variables x, y, and z are assigned the values
5, 6, and 11:</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x +
y;
document.
getElemen
tById("de
mo").inner
HTML =
z;
The JavaScript syntax defines two
types of values: Fixed values and
variable values.
Other are
char, String, Array, toString, NaN,
valueOf, and more…
Declaration in js:
function example() {
var a = 1; // Function-scoped variable
let b = 2; // Block-scoped variable
const c = 3; // Block-scoped constant
if (true) {
var a = 4; // Overwrites the previous
value of a
let b = 5; // Scoped only to this block
// const c = 6; // Error: Re-declaring a
constant in the same block
console.log(a); // 4
console.log(b); // 5
}
lastName = "Doe";
lastname =
"Peterson";
JavaScript uses the Unicode character
set.
Unicode covers (almost) all the
characters, punctuations, and
symbols in the world
JavaScript Assignment Operators
Shift Assignment Operators
Bitwise Assignment Operators
Logical Assignment Operators
Data Types
JavaScript has 8 Datatypes:
String
Number
Bigint
Boolean
Undefined
Null
Symbol
Object
JavaScript Function Syntax
Object Properties
• A real life car has properties like weight
and color:
• car.name = Fiat, car.model = 500,
car.weight = 850kg, car.color = white.
• Car objects have the same properties,
but the values differ from car to car.
Creating a JavaScript Object
1. // Create an Object
const person = {firstName:"John", lastName:"Doe", age:50,
eyeColor:"blue"};
2. // Create an Object
const person = {};
// Add Properties
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
3. // Create an Object
const person = new Object();
// Add Properties
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
Accessing Object Properties
• objectName.propertyName
• objectName["propertyName"]
const person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + "
" + this.lastName;
}
};
JavaScript Objects are Mutable
//Create an Object
const person = {
firstName:"John",
lastName:"Doe",
age:50, eyeColor:"blue"
}
// Create a copy
const x = person;
<p id="demo"></p>
<script>
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
};
document.getElementById("demo").innerHTML = person.firstname
+ " is " + person.age + " years old.";
</script>
</body>
</html>
Adding New Properties
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Object Properties</h1>
<h2>Adding a new Property</h2>
<p id="demo"></p>
<script>
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
};
person.nationality = "English";
document.getElementById("demo").innerHTML =
person.firstname + " is " + person.nationality + ".";
</script>
</body>
</html>
Deleting Properties
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Object Properties</h1>
<h2>Deleting a Property</h2>
<p id="demo"></p>
<script>
const person = {
firstname: "John",
lastname: "Doe",
age: 50,
eyecolor: "blue"
};
delete person.age;
document.getElementById("demo").innerHTML =
person.firstname + " is " + person.age + " years old.";
</script>
</body>
</html>
Nested Objects
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Objects</h1>
<h2>Accessing Nested Objects</h2>
<p id="demo"></p>
<script>
// Create nested Objects
const myObj = {
name: "John",
age: 30,
myCars: {
car1: "Ford",
car2: "BMW",
car3: "Fiat"
}
}
document.getElementById("demo").innerHTML =
myObj.myCars.car2;
</script>
</body>
</html>
Accessing Object Methods
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Objects</h1>
<h2>Object Methods</h2>
<p>A method is a function definition stored as a property
value.</p>
<p id="demo"></p>
<script>
const person = {
firstName: "John",
lastName: "Doe",
id: 5566,
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
document.getElementById("demo").innerHTML =
person.fullName();
</script>
</body>
Displaying Object Properties:
<p id="demo"></p>
<script>
// Create an Object
const person = {
name: "John",
age: 30,
city: "New York"
};
// Display Properties
document.getElementById("demo").innerHTML = person.name + ",
" + person.age + ", " + person.city;
</script>
</body>
</html>
Displaying Properties in a Loop
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Objects</h1>
<h2>Display Properties</h2>
<p id="demo"></p>
<script>
// Create an Object
const person = {
name: "John",
age: 30,
city: "New York"
};
// Build a Text
let text = "";
for (let x in person) {
text += person[x] + " ";
};
</body>
</html>
Using Object.values()
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Objects</h1>
<h2>The Object.values() Method</h2>
<p>Object.values() returns an array of values from an object:</p>
<p id="demo"></p>
<script>
// Create an Object
const person = {
name: "John",
age: 30,
city: "New York"
};
// Create an Array
const myArray = Object.values(person);
</body>
</html>
Using JSON.stringify()
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Objects</h1>
<h2>Display Properties with JSON</h2>
<p id="demo"></p>
<script>
// Create an Object
const person = {
name: "John",
age: 30,
city: "New York"
};
// Display JSON
document.getElementById("demo").innerHTML = JSON.stringify(person);
</script>
</body>
</html>
JavaScript String Methods
JavaScript Arrays
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
const cars = ["Saab","Volvo","BMW"];
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
Converting an Array to a String
const fruits =
["Banana", "Orange", "Apple", "M
ango"];
let fruit = fruits[0];
Accessing the Last Array Element
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple",
"Mango"];
document.getElementById("demo").innerHTML =
text;
function myFunction(value) {
text += "<li>" + value + "</li>";
}
</script>
</body>
</html>
Adding Array Elements
New element can also be added to an array using the length property:
Checks if an array
includes() contains a specified value, arr.includes(3) true or false
returns true or false.
// 1. forEach
arr.forEach(num => console.log(num)); // Logs 1, 2, 3, 4, 5
// 2. map
const doubled = arr.map(num => num * 2); // [2, 4, 6, 8, 10]
// 3. filter
const evenNumbers = arr.filter(num => num % 2 === 0); // [2, 4]
// 4. reduce
const total = arr.reduce((acc, num) => acc + num, 0); // 15
// 5. some
const hasOdd = arr.some(num => num % 2 !== 0); // true
// 6. every
const allPositive = arr.every(num => num > 0); // true
// 8. flatMap
const nestedArr = ["a b", "c d"];
const words = nestedArr.flatMap(str => str.split(" ")); // ['a', 'b', 'c', 'd']
// 9. with
const newArr = arr.with(2, 99); // [1, 2, 99, 4, 5]
HTML Events
JavaScript Event Handlers
Event Handler:
cript>
onst button = document.getElementById("myButton");
button.addEventListener("click", function () {
alert("Button clicked!");
);
script>
Using HTML DOM Properties:
<button id="myButton">Click
Me</button>
<script>
const button =
document.getElementById("myButton");
button.onclick = function () {
alert("Button clicked!");
};
</script>
JavaScript Strings