Unit 2-2 - Web Technology
Unit 2-2 - Web Technology
B.Tech – V Semester
UNIT II
M. Vijayakumar
Assistant Professor (SS)
School of Computing Sciences,
Department of Computer Science and Engineering
Functions
• Function names can contain letters, digits, underscores, and dollar signs (same rules as
variables).
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.
You can reuse code: Define the code once, and use it many times.
You can use the same code many times with different arguments, to produce
different results.
Function Return
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute the
code after the invoking statement.
Functions often compute a return value. The return value is "returned" back to the
"caller":
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 6
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p id="demo"></p>
<script>
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
function myFunction(a, b) {
return a * b;
}
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 7
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
<body>
<body>
<h2>JavaScript Functions</h2>
<h2>JavaScript Functions</h2>
<p>Accessing a function without () will return the function
<p>This example calls a function to convert from
definition instead of the function result:</p>
Fahrenheit to Celsius:</p>
<p id="demo"></p>
<p id="demo"></p>
<script>
<script>
function toCelsius(f) {
function toCelsius(f) {
return (5/9) * (f-32);
return (5/9) * (f-32);
}
}
document.getElementById("demo").innerHTML =
document.getElementById("demo").innerHTML =
toCelsius;
toCelsius(77);
</script>
</script>
</body>
</body>
</html>
</html>
Using the example above, toCelsius refers to the function object, and toCelsius()
refers to the function result.
Accessing a function without () will return the function object instead of the function
result.
Functions can be used the same way as you use variables, in all types of formulas,
assignments, and calculations.
<h2>JavaScript Functions</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The temperature is " + toCelsius(77) + " Celsius";
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
</script>
</body>
</html>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
const cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
If you have a list of items (a list of car names, for example), storing the cars in single
variables could look like this:
et car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";
However, what if you want to loop through the cars and find a specific one? And what if
you had not 3 cars, but 300?
An array can hold many values under a single name, and you can access the values by
referring to an index number.
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 16
Creating an Array
Syntax:
The following example also creates an Array, and assigns values to it:
<!DOCTYPE html>
<html>
<body>
The two examples above do
<h2>JavaScript Arrays</h2> exactly the same.
<p id="demo"></p>
There is no need to use new
<script> Array().
const cars = new Array("Saab", "Volvo", "BMW");
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
const cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 20
Changing an Array Element
cars[0] = "Opel";
This statement changes the value of the first element in cars:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
const cars = ["Saab", "Volvo", "BMW"];
cars[0] = "Opel";
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 21
Access the Full Array
With JavaScript, the full array can be accessed by referring to the array name:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
const cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
Arrays use numbers to access its "elements". In this example, person[0] returns John:
Because of this, you can have variables of different types in the same Array.
You can have objects in an Array. You can have functions in an Array. You can have arrays
in an Array:
myArray[0] = Date.now;
myArray[1] = myFunction;
myArray[2] = myCars;
</body>
</html>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits[0];
</script>
</body>
</html>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML =
fruits[fruits.length-1];
</script>
</body>
</html>
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML =
fruits.toString();
</script>
</body>
</html>
It behaves just like toString(), but in addition you can specify the separator:
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" *
");
</script>
</body>
</html>
Popping
The pop() method removes the last element from an array:
<p id="demo1"></p>
<p id="demo2"></p>
The pop() method
returns the value
<script> that was "popped
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
out":
fruits.pop();
document.getElementById("demo2").innerHTML = fruits;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 36
<!DOCTYPE html>
<html>
<body>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
document.getElementById("demo2").innerHTML = fruits.pop();
document.getElementById("demo3").innerHTML = fruits;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 37
<!DOCTYPE html>
<html>
Pushing <body>
The push()
method adds a <h2>JavaScript Array Methods</h2>
<h2>push()</h2>
new element to <p>The push() method appends a new element to an array.</p>
an array (at the
end): <button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.push("Kiwi");
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 38
<!DOCTYPE html>
<html>
<body>
function myFunction() {
document.getElementById("demo1").innerHTML = fruits.push("Kiwi");
document.getElementById("demo2").innerHTML = fruits;
}
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 39
Changing Elements
Array elements are accessed using their index number:
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo1").innerHTML = fruits;
fruits[0] = "Kiwi";
document.getElementById("demo2").innerHTML = fruits;
</script>
</body>
</html>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 42
<!DOCTYPE html>
<html>
Deleting <body>
Elements
Since JavaScript <h2>JavaScript Array Methods</h2>
<p>Deleting elements leaves undefined holes in an array.</p>
arrays are objects, <p id="demo1"></p>
elements can be <p id="demo2"></p>
deleted by using <script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
the JavaScript
operator delete: document.getElementById("demo1").innerHTML =
"The first fruit is: " + fruits[0];
delete fruits[0];
document.getElementById("demo2").innerHTML =
"The first fruit is: " + fruits[0];
</script>
</body>
</html>
• The first parameter (2) defines the position where new elements should be added (spliced
in).
• The second parameter (0) defines how many elements should be removed.
• The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.
</body>
</html>
• The first parameter (0) defines the position where new elements should be added
(spliced in).
• The second parameter (1) defines how many elements should be removed.
• The rest of the parameters are omitted. No new elements will be added.
The concat() method does not change the existing arrays. It always returns a new
array.
<p id="demo"></p>
<script>
const myGirls = ["Cecilie", "Lone"];
const myBoys = ["Emil", "Tobias", "Linus"];
const myChildren = myGirls.concat(myBoys);
document.getElementById("demo").innerHTML = myChildren;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 48
<!DOCTYPE html>
<html>
<body>
document.getElementById("demo").innerHTML = myChildren;
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 49