Q1 : Create one function with zero parameter having a console statement;
ANS :-
function question(){
console.log("This is Zero peremeter Function")
}
question();
Output :- This is Zero peremeter Function
Q2 : Create one function which takes two values as a parameter and print the sum of them as "Sum of 3, 4 is 7"
ANS :-
function question2(a,b){
let sum = a+b;
console.log(sum)
}
question2(3,4)
Output :- 7
Q3 : Create one arrow function
ANS :-
const arrow=()=>{
console.log("This is arrow function ");
}
arrow();
Output :- This is arrow function
Q4 : print Output
var x = 21;
var girl = function () {
console.log(x);
var x = 20;
};
girl ();
Output :- undefined
NOTE :-
within the function scope x gets hoisted and since it is of var type, we get the output as undefined
Q5 : print Output
var x = 21;
girl ();
console.log(x)
function girl() {
console.log(x);
var x = 20;
};
Output :-
undefined
21
NOTE :-
Due to hoisting of the variable within the function scope, first we get the output as undefined. x has been initialized as 21 already and that is printed when console.log(x) is executed
Q6 : Print Output
var x = 21;
a();
b();
function a() {
x = 20;
console.log(x);
};
function b() {
x = 40;
console.log(x);
};
Output :-
20
40
NOTE :-
value of x is initialized as 20 within the function scope of a() and output is 20. value of x is initialized as 40 within the function scope of b(), so the output is 40.
Q7 : Write a function that accepts parameter n and returns factorial of n
const fact=(num)=>{
ans=1
if(num>0){
for (let i=1; i<=num;i++){
ans=ans*i
}
console.log(ans)
}
else{
console.log("enter a postive no")
}
}
fact(5)
Output :- 120