Ch 4 - JavaScript Functions
Ch 4 - JavaScript Functions
Chapter 4
JavaScript: Functions
Group: 5
3rd Year Sec B Students,
Dept. of Software Eng.
• Components of Function
• Types of Function
• Hoisting
function findMax() {
let max = -Infinity;
for(let i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
console.log(findMax(2, 3, 4, 5, 6)) // 6 is output
}
<button
<button onclick="greeter.sayHello()">say
onclick="sayHello()">say hello</button>
hello</button> <script>
<script> greeter = {
function sayHello() { sayHello: function () {
console.log(this); console.log(this);
console.log(arguments); console.log(arguments);
console.log('hello'); console.log('hello');
} }
</script> }
▪ A function expression is very similar to and has almost the same syntax as a
function declaration.
▪ The main difference between a function expression and a function declaration is
that it can be stored in a variable.
▪ Such a function can be anonymous; it does not have to have a name.
const square = function(number) { return number * number }
var x = square(4)
▪ Also by Providing a name allows the function to refer to itself, and also makes it
easier to identify the function in a debugger's stack traces:
const factorial = function fac(n) { return n < 2 ? 1 : n * fac(n - 1) }
console.log(factorial(3))
3. param => {
let a = 1;
return a + param;
}
▪ Variables defined inside a function cannot be accessed from anywhere outside the function
▪ A function defined inside another function can also access all variables defined in its
parent function, and any other variables to which the parent function has access
JavaScript has 3 types of scope:
● Block scope
● Function scope
● Global scope
function myFunction() { ★ A function body creates a scope
let carName ="Volvo";
//Function Scope for let, const and even function
} declarations.
● In JavaScript, accessor properties are methods that get or set the value of an object. For that,
we use these two keywords:
get - to define a getter method to get the property value
- getter methods are used to access the properties of an object
set - to define a setter method to set the property value
- setter methods are used to change the values of an object.
const student = { firstName: 'Monica', const student = { firstName: 'Monica',
// accessor property(getter) //accessor property(setter)
get getName() {return this.firstName; } set changeName(newName) {
}; this.firstName = newName;
console.log(student.firstName); // Monica }
// accessing getter methods };
console.log(student.getName); // Monica console.log(student.firstName); // Monica
// trying to access as a method // change(set) object property using a setter
console.log(student.getName()); // error student.changeName = 'Sarah';
console.log(student.firstName); // Sarah
a = 5; greet();
console.log(a);
function greet() {
var a; // 5 console.log('Hi, there.');
}
Object methods
Object.create() is used to create a new object and link it to the prototype of an existing
object.
Math methods
log() Returns the natural logarithm (base E) of a number.
max() and min() Returns the largest and smallest of zero or more numbers repectively.
exp() Returns EN, where N is the argument, and E is Euler's constant, the base of
the natural logarithm.
Date methods
Date() returns a string representation of the current date and time
getDate() and Returns the day of the month/weekfor the specified date according to
local time respectively.
getDay()
setDate() Sets the day of the month for a specified date according to local time.
getUTCDate() Returns the day (date) of the month in the specified date according to
universal time.
getTimezoneOffset() Returns the time-zone offset in minutes for the current locale.
toSource() Returns a string representing the source for an equivalent Date object;
you can use this value to create a new object.
Internet Programming I Chapter 4 - JavaScript 24
Practical Exercises
1. What is the difference between JavaScript function declaration and function
expression.
2. What is The difference between getter and setter methods in javascript?
3. What is The argument object?
4. What is Hoisting?
5. Write a JavaScript function that accepts two arguments, a string and a letter
and the function will count the number of occurrences of the specified letter
within the string?
6. Write a JavaScript program to pass a 'JavaScript function' as parameter?
7. Write a JavaScript function that checks whether a passed string is palindrome
or not?
object.create() =>
object.key() ⇑
Links
✔ https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Statements/function
✔ https://www.w3schools.com/js/js_scope.asp
✔ https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Functions/get