Web Moduel 2 (Javascript) MMC105
Web Moduel 2 (Javascript) MMC105
What is JavaScript
JavaScript (js) is a light-weight object-oriented programming language
which is used by several websites for scripting the webpages. It is an
interpreted, full-fledged programming language that enables dynamic
interactivity on websites when applied to an HTML document. It was
introduced in the year 1995 for adding programs to the webpages in the
Netscape Navigator browser. Since then, it has been adopted by all other
graphical web browsers. With JavaScript, users can build modern web
applications to interact directly without reloading the page every time.
The traditional website uses js to provide several forms of interactivity
and simplicity.
Features of JavaScript
1 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
History of JavaScript
In 1993, Mosaic, the first popular web browser, came into existence. In
the year 1994, Netscape was founded by Marc Andreessen. He
realized that the web needed to become more dynamic. Thus, a 'glue
language' was believed to be provided to HTML to make web designing
easy for designers and part-time programmers. Consequently, in 1995,
the company recruited Brendan Eich intending to implement and embed
Scheme programming language to the browser. But, before Brendan could
start, the company merged with Sun Microsystems for adding Java into
its Navigator so that it could compete with Microsoft over the web
technologies and platforms. Now, two languages were there: Java and the
scripting language. Further, Netscape decided to give a similar name to
the scripting language as Java's. It led to 'Javascript'. Finally, in May 1995,
Marc Andreessen coined the first code of Javascript named 'Mocha'. Later,
the marketing team replaced the name with 'LiveScript'. But, due to
trademark reasons and certain other reasons, in December 1995, the
language was finally renamed to 'JavaScript'. From then, JavaScript came
into existence.
Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:
2 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
o Client-side validation,
o Dynamic drop-down menus,
o Displaying date and time,
o Displaying pop-up windows and dialog boxes (like an alert dialog
box, confirm dialog box and prompt dialog box),
o Displaying clocks etc.
JavaScript Example
1. <script>
2. document.write("Hello JavaScript by JavaScript");
3. </script>
1. <script type="text/javascript">
2. alert("Hello Javatpoint");
3. </script>
Test it Now
To call function, you need to work on event. Here we are using onclick
event to call msg() function.
1. <html>
2. <head>
3. <script type="text/javascript">
4. function msg(){
5. alert("Hello Javatpoint");
6. }
7. </script>
8. </head>
9. <body>
10. <p>Welcome to JavaScript</p>
11. <form>
12. <input type="button" value="click" onclick="msg()"/>
13. </form>
14. </body>
15. </html>
message.js
1. function msg(){
2. alert("Hello Javatpoint");
3. }
Let's include the JavaScript file into html page. It calls the JavaScript
function on button click.
index.html
4 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
1. <html>
2. <head>
3. <script type="text/javascript" src="message.js"></script>
4. </head>
5. <body>
6. <p>Welcome to JavaScript</p>
7. <form>
8. <input type="button" value="click" onclick="msg()"/>
9. </form>
10. </body>
11. </html>
1. The stealer may download the coder's code using the url of the js
file.
2. If two js files are dependent on one another, then a failure in one file
may affect the execution of the other dependent file.
3. The web browser needs to make an additional http request to get
the js code.
5 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
JavaScript Variables
Variables in JavaScript can be declared using var, let, or const. JavaScript is dynamically typed, so
variable types are determined at runtime without explicit type definitions.
console.log(a);
console.log(b);
console.log(c);
Output
10
20
30
6 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
var is a keyword in JavaScript used to declare variables and it is Function-scoped and hoisted,
allowing redeclaration but can lead to unexpected bugs.
var b = 10;
console.log(a);
console.log(b);
let is a keyword in JavaScript used to declare variables and it is Block-scoped and not hoisted to the
top, suitable for mutable variables
let a = 12
let b = "gfg";
console.log(a);
console.log(b);
const is a keyword in JavaScript used to declare variables and it is Block-scoped, immutable bindings
that can’t be reassigned, though objects can still be mutated.
const a = 5
let b = "gfg";
console.log(a);
console.log(b);
7 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
Variable names must begin with a letter, underscore (_), or dollar sign ($).
Subsequent characters can be letters, numbers, underscores, or dollar signs.
Variable names are case-sensitive (e.g., age and Age are different variables).
Reserved keywords (like function, class, return, etc.) cannot be used as variable names.
Controls Statements
JavaScript If-else
The JavaScript if-else statement is used to execute the code whether
condition is true or false. There are three forms of if statement in
JavaScript.
1. If Statement
2. If else statement
3. if else if statement
JavaScript If statement
It evaluates the content only if expression is true. The signature of
JavaScript if statement is given below.
1. if(expression){
2. //content to be evaluated
3. }
8 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
1. <script>
2. var a=20;
3. if(a>10){
4. document.write("value of a is greater than 10");
5. }
6. </script>
1. if(expression){
2. //content to be evaluated if condition is true
3. }
4. else{
5. //content to be evaluated if condition is false
6. }
Let’s see the example of if-else statement in JavaScript to find out the
even or odd number.
1. <script>
2. var a=20;
3. if(a%2==0){
4. document.write("a is even number");
5. }
6. else{
7. document.write("a is odd number");
10 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
8. }
9. </script>
1. if(expression1){
2. //content to be evaluated if expression1 is true
3. }
4. else if(expression2){
5. //content to be evaluated if expression2 is true
6. }
7. else if(expression3){
8. //content to be evaluated if expression3 is true
9. }
10.else{
11.//content to be evaluated if no expression is true
12.}
1. <script>
2. var a=20;
3. if(a==10){
4. document.write("a is equal to 10");
5. }
6. else if(a==15){
7. document.write("a is equal to 15");
8. }
9. else if(a==20){
10.document.write("a is equal to 20");
11.}
12.else{
11 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
JavaScript Loops
Output
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Let’s now discuss the different types of loops available in
JavaScript
1. JavaScript for Loop
The for loop repeats a block of code a specific number of times. It
contains initialization, condition, and increment/decrement in one
line.
Syntax
for (initialization; condition; increment/decrement) {
// Code to execute
}
12 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
Output
Count: 1
Count: 2
Count: 3
In this example
Initializes the counter variable (let i = 1).
Tests the condition (i <= 3); runs while true.
Executes the loop body and increments the counter (i++).
2. JavaScript while Loop
The while loop executes as long as the condition is true. It can be
thought of as a repeating if statement.
Syntax
while (condition) {
// Code to execute
}
let i = 0;
13 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
while (i < 3) {
console.log("Number:", i);
i++;
}
Output
Number: 0
Number: 1
Number: 2
In this example
Initializes the variable (let i = 0).
Runs the loop body while the condition (i < 3) is true.
Increments the counter after each iteration (i++).
3. JavaScript do-while Loop
The do-while loop is similar to while loop except it executes the
code block at least once before checking the condition.
Syntax
do {
// Code to execute
} while (condition);
let i = 0;
do {
console.log("Iteration:", i);
i++;
} while (i < 3);
Output
Iteration: 0
14 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
Iteration: 1
Iteration: 2
In this example:
Executes the code block first.
Checks the condition (i < 3) after each iteration.
4. JavaScript for-in Loop
The for…in loop is used to iterate over the properties of an object.
It only iterate over keys of an object which have their enumerable
property set to “true”.
Syntax
for (let key in object) {
// Code to execute
}
const obj = { name: "Ashish", age: 25 };
for (let key in obj) {
console.log(key, ":", obj[key]);
}
Output
name : Ashish
age : 25
In this example:
Iterates over the keys of the person object.
Accesses both keys and values.
5. JavaScript for-of Loop
The for…of loop is used to iterate over iterable objects like arrays,
strings, or sets. It directly iterate the value and has more concise
syntax than for loop.
Syntax
for (let value of iterable) {
// Code to execute
}
let a = [1, 2, 3, 4, 5];
for (let val of a) {
console.log(val);
}
Output
1
2
3
15 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
4
5
JavaScript Array
JavaScript array is an object that represents a collection of similar type
of elements.
1. By array literal
2. By creating instance of Array directly (using new keyword)
3. By using an Array constructor (using new keyword)
1. var arrayname=[value1,value2.....valueN];
Let's see the simple example of creating and using array in JavaScript.
1. <script>
2. var emp=["Sonoo","Vimal","Ratan"];
3. for (i=0;i<emp.length;i++){
4. document.write(emp[i] + "<br/>");
5. }
6. </script>
Test it Now
16 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
Sonoo
Vimal
Ratan
1. <script>
2. var i;
3. var emp = new Array();
4. emp[0] = "Arun";
5. emp[1] = "Varun";
6. emp[2] = "John";
7.
8. for (i=0;i<emp.length;i++){
9. document.write(emp[i] + "<br>");
10. }
11. </script>
Test it Now
Arun
Varun
John
17 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
1. <script>
2. var emp=new Array("Jai","Vijay","Smith");
3. for (i=0;i<emp.length;i++){
4. document.write(emp[i] + "<br>");
5. }
6. </script>
Test it Now
Jai
Vijay
Smith
Methods Description
18 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
array.
19 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
20 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
JavaScript Functions
JavaScript functions are used to perform operations. We can call
JavaScript function many times to reuse the code.
Let’s see the simple example of function in JavaScript that does not has
arguments.
1. <script>
2. function msg(){
3. alert("hello! this is message");
4. }
5. </script>
6. <input type="button" onclick="msg()" value="call function"/>
1. <script>
2. function getcube(number){
3. alert(number*number*number);
4. }
5. </script>
6. <form>
7. <input type="button" value="click" onclick="getcube(4)"/>
8. </form>
1. <script>
2. function getInfo(){
3. return "hello javatpoint! How r u?";
4. }
5. </script>
6. <script>
7. document.write(getInfo());
8. </script>
22 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
Syntax
1. new Function ([arg1[, arg2[, ....argn]],] functionBody)
Parameter
arg1, arg2, .... , argn - It represents the argument used by function.
ADVERTISEMENT
Method Description
23 PROF.SACHIN.G.A,
CIT,MANDYA
MMC105 WEB TECHNOLOGIES(JAVASCRIPT)
1. <script>
2. var add=new Function("num1","num2","return num1+num2");
3. document.writeln(add(2,5));
4. </script>
Output:
Example 2
Let's see an example to display the power of provided value.
1. <script>
2. var pow=new Function("num1","num2","return Math.pow(num1,num2)");
3. document.writeln(pow(2,3));
4. </script>
Output:
24 PROF.SACHIN.G.A,
CIT,MANDYA