Functions
◾Functions are one of the
fundamental building blocks in Java Script.It is a block of code designed to perform
particular task.
◾Why Function?
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.
◾Examples:
There are different way to define a function:
1.
//<****first WAY***>
//name of fuction is sayhello
let sayHello = function(name){
console.log(`Greeting msg for ${name}`)
//use the $ sign to fetch value of variable
console.log(`hii ${name}`)
}
//calling fuction
sayHello('pritpal');
2.
//<------2nd---------------->
let fullNameMaker = function(fname,lname){
console.log('WELCOME')
console.log(`happy to have you, ${fname} ${lname}`)
}
//calling
fullNameMaker('Pritpal','Dhillon')
3.
//<-----------3rd----------->
//function to passing arrguments
let myAdder = function(num1,num2){
let added = num1 + num2
return added
}
//2 ways to print
// let result = myAdder(3,6)
// console.log(result)
console.log(myAdder(3,6))
4.
//<---------Default Parameter ------------->
let guestUser = function(name = 'BCA',course = '2'){
return 'hello ' + name + ' and ur course is '+course
}
console.log(guestUser('MCA',1)) //BCA is going to
replaced by MCA
0 Comments