Javascript 1 24102024 094110am
Javascript 1 24102024 094110am
Basics of JavaScript
1
INTRODUCTION
▷ A scripting language developed by
Netscape to enable Web authors to design
interactive sites.
▷ They can be written right in the HTML and
execute automatically as the page loads.
▷ A lightweight programming language that
runs in a Web browser(client-side).
▷ The programs in this language are
called scripts.
▷ Supports event-driven programming
▷ JavaScript code must be inserted between
<script> and </script> tags
HTML CSS JavaScript
▷ 4 . Event Handling:
JavaScript provides event-driven
programming, allowing the web page to
react to user events (e.g., button clicks,
form submissions, scrolling) and execute
code in response.
Example:
▷ Displaying error messages when a form
is submitted without required fields.
Case Sensitivity
▷ HTML is not case sensitive. The following
mean the same to the browser:
○ <HTML> -- <html>
○ <Html> -- <htMl>
▷ JavaScript is case sensitive. Only the first of
the following will result in the desired function
– the rest will generate an error or some other
undesirable event:
○ onMouseClick -- OnMouseClick
○ onmouseclick -- ONMOUSECLICK
alert(“hello world”);
Alert(“hello world”);
7
Example Code
<html>
<head>
<title>A First Program in JavaScript</title>
</head>
<body>
<script>
document.writeln("<h1>Welcome to
JavaScript Programming!</h1>" );
</script>
</body>
</html>
Ways to Output Data in
JavaScript
11
12
JavaScript DataTypes:
16
17
Empty Value is also called Null Value
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).
The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets: {}
Function parameters are listed inside the parentheses () in the function definition.
Function arguments are the values received by the function when it is invoked.
Inside the function, the arguments (the parameters) behave as local variables.
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
// This is allowed:
person.age = 10; // Changing a property of the object
58
Control Statements
▷ Selection statements :
The selection statements (if-else and if-then-else)
are similar to those programming languages.
59
60
The switch statement
It is similar as other programming
languages.
61
Loop Statements
While loop :
While (control expression) {statements}
▷ For loop :
For(initial expr ; control expr ; increment)
{ //statements}
do { //statements } while (control
expression)
62
Screen output and
Keyboard input
▷ JavaScript represents the HTML document with
the document object.
▷ The window in which the browser displays an
html document is represent with window
object.
▷ The window object includes two properties the
window and document properties.
▷ The document object have several properties
and methods.
63
64
Window Object
▷ The window object represents an open
window in a browser.
▷ The window object is supported by all
browsers
▷ Global variables are properties of the window
object.
▷ Global functions are methods of the window
object.
65
Window object Methods
66
Window object Methods
67
Window object Properties
68
Window Object Properties
69
Popup Boxes
▷ Alert Box
window.alert(“Hello Every body!!");
▷ Confirm Box
window.confirm("Press a button");
▷ Prompt Box
window.prompt(“Your name","")
70
Some Additional Info:
72