Professional Documents
Culture Documents
Final Web Application
Final Web Application
Final Web Application
document.getElementById("demo").innerHTML = "Paragra
ph changed.";
}
• Loading the External JS Files:
<script src="myScript.js"></script>
Showing Output using JavaScript
We can use these four ways to show output using JavaScript:
• Writing into an HTML element, using innerHTML
• Writing into the HTML output using document.write()
• Writing into an alert box, using window.alert()
• Writing into the browser console, using console.log()
Showing Output in innerHTML
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Showing Output using document.write()
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
Showing Output using window.alert()
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Showing Output using console.log()
<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
Printing Existing Content
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Why do we use JavaScript in Web
Development?
• To add dynamic behavior to the webpage
• To create special effect on the webpage
• To make interactive webpage
• To make web apps
JavaScript Basics: Like Any Programming
Language!
• Comments
• Variable
• Global Variable
• Data Type
• Operators
• If Statement
• Switch
• Loop
• Function
JS Comments
• There are two types of comments in JavaScript:
1. Single-line Comment:
<script>
// It is single line comment
document.write("hello javascript");
</script>
2. Multi-line Comment:
<script>
/* It is multi line comment.
It will not be displayed */
document.write("example of javascript multiline comment");
</script>
JS Variable
• A JavaScript variable is simply a name of storage location. There are
two types of variables in JavaScript :
1. Local variable and
2. Global variable.
Output:
for (initialization; condition; increment) 1
{ 2
code to be executed 3
} 4
5
JavaScript while loop <script>
var i=11;
while (i<=15)
• The JavaScript while loop iterates the {
elements for the infinite number of document.write(i + "<br/>");
times. It should be used if number of i++;
iteration is not known. The syntax of }
while loop is given below. </script>
Output:
while (condition)
11
{
12
code to be executed 13
} 14
15
JavaScript do while loop <script>
var i=21;
do{
• The JavaScript do while loop iterates document.write(i + "<br/>");
the elements for the infinite number i++;
of times like while loop. But, code }while (i<=25);
is executed at least once whether </script>
condition is true or false. The syntax
of do while loop is given below. Output:
21
22
do{
23
code to be executed 24
}while (condition); 25
JavaScript Functions
JavaScript functions are used to perform operations. We can call
JavaScript function many times to reuse the code.
Advantage of JavaScript function
• There are mainly two advantages of JavaScript functions:
1. Code reusability: We can call a function several times so it save coding.
2. Less coding: It makes our program compact. We don’t need to write many
lines of code each time to perform a common task.
• JS Function Syntax
function functionName([arg1, arg2, ...argN]){
//code to be executed
}
JavaScript Function Example
<script>
function msg(){
alert("hello! this is message");
}
</script>
<form>
<input type="button" value="click" onclick="getcube(4)"/>
</form>
JavaScript Function with Return Value Example
<script>
function getInfo(){
return "hello javatpoint! How r u?";
}
</script>
<script>
document.write(getInfo());
</script>
JavaScript Object
• In JavaScript objects are Object Properties
variables too. But objects The name:values pairs in JavaScript
can contain many values. objects are called properties:
• var person = {
firstName: "John", Property Property Value
lastName: "Doe", firstName John
age: 50,
eyeColor: "blue" lastName Doe
}; age 50
eyeColor blue
Object Method
JavaScript Object • Objects can also have methods.
• Methods are actions that can be
performed on objects.
var person = {
• Methods are stored in properties
firstName: "John", as function definitions.
lastName : "Doe",
id : 5566, Property Property Value
fullName : function() {
return this.firstName firstName John
+ " " + this.lastName; lastName Doe
} age 50
};
eyeColor blue
fullName function() {return
this.firstName + " " +
this.lastName;}
JavaScript Object Example in Code
JavaScript Array
• An array is a special
variable, which can
hold more than one
value
DOM Method
• Elements by id: document.getElementById("intro")