Java Script PPT(Unit-3)
Java Script PPT(Unit-3)
Java Script is used insert dynamic text into HTML (ex: user
name)
Java script offers a vast standard library that has a wide variety
of functions and methods available to help in the process of
development, making the entire process easier and hassle-free
It was developed with the main intention of DOM (Document Object Model)- to
change or modify an HTML document that will be displayed in a web
browser.) manipulation, bringing forth the era of dynamic websites.
// for...of Loop
for (let value of numArray) {
console.log(value);
}
6/7/2023 49
JS Popup boxes(cont..)
Prompt Dialog box
The prompt dialog box is used when it is required to pop-up a text box for
getting the user input.
It provides an input field where the user can enter a value. The entered
value is captured as a string.
var userInput = prompt("Please enter your name:", “SHIV VEER");
if (userInput !== null)
{ // User entered a value
console.log("Hello, " + userInput + "!"); }
else
{ // User clicked Cancel or closed the prompt
console.log("User canceled or did not enter a value."); }
You can assign your own event handler functions to HTML elements
• Creating Objects:
• There are multiple ways to create objects in JavaScript.
The most common methods are:
1. Object Literal Syntax: This is the simplest way to
create an object. We define key-value pairs within curly
braces { }.
const person = {
firstName: “SHIV VEER", Name: “SINGH", age: 43,
isEmployed: true,
};6/7/2023 Prof. SHIV VEER SINGH 68
JavaScript Objects
• Object Methods:
Objects can also contain functions, known as
methods:
const calculator = {
add: function (a, b) {
return a + b; },
subtract: function (a, b) {
return a - b; }, };
console.log(calculator.add(5, 3));
console.log(calculator.subtract(10, 4));
6/7/2023 Prof. SHIV VEER SINGH 74
JavaScript Objects
// ES6
const x = (x, y) => x * y;
Arrow functions do not have their own this. They are not well suited for defining object
methods
Arrow functions are not hoisted. They must be defined before they are used
Using const is safer than using var, because a function expression is always constant
value.
You can only omit the return keyword and the curly brackets if the function is a single
statement. Because of this, it might be a good habit to always keep them:
Example
const x = (x, y) => { return x * y };
6/7/2023 Prof. SHIV VEER SINGH 80
JavaScript Function Parameters
A JavaScript function does not perform any checking on parameter values (arguments).
function functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
Function parameters are the names listed in the function definition.
Function arguments are the real values passed to (and received by) the function
Parameter Rules
• JavaScript function definitions do not specify data types for parameters.
• JavaScript functions do not perform type checking on the passed arguments.
• JavaScript functions do not check the number of arguments received.
Default Parameters
• If a function is called with missing arguments (less than declared), the missing values
are set to undefined
function myFunction(x, y) {
if (y === undefined) {
y = 2;
6/7/2023 Prof. SHIV }
VEER SINGH 81
}
Form Validation Example
• <!DOCTYPE html>
• <html>
• <head>
• <title>Form Validation Example</title>
• <style>
• .error {
• color: red;
• }
• </style>
• </head>
6/7/2023 Prof. SHIV VEER SINGH 82
Form Validation Example Cont..
• <body>
• <h1>Form Validation Example</h1>
• <form id="myForm">
• <label for="name">Name:</label>
• <input type="text" id="name" name="name"><span id="nameError"
class="error"></span><br>
• <label for="email">Email:</label>
• <input type="text" id="email" name="email"><span id="emailError"
class="error"></span><br>
• <label for="password">Password:</label>
• <input type="password" id="password" name="password"><span id="passwordError"
class="error"></span><br>
• <script>
• const form = document.getElementById("myForm");
• const nameInput = document.getElementById("name");
• const emailInput = document.getElementById("email");
• const passwordInput = document.getElementById("password");
• const nameError = document.getElementById("nameError");
• const emailError = document.getElementById("emailError");
• const passwordError = document.getElementById("passwordError");
class Person {
constructor() {
console.log("Constructor is called")
}
}
let p1= new Person() //contructor is called
class Person {
firstName:string;
lastName:string;
constructor(fName:string, lName:string) {
this.firstName=fName;
this.lastName=lName;
}
}
let a="01"
let b=1
console.log(a==b);//true