ExcelR Frontend by Samba
Example17:
<script>
let func_one=param1=>param1;
let res=func_one("ReactJS");
document.write(res);
// /ReactJS
</script>
Example18:
<script>
let obj={
sub:``,
my_func:function(){
let inner_func=()=>{
this.sub=`ReactJS`;
}
inner_func();
}
};
obj.my_func();
document.write(obj.sub);
//Arrow functions ===> ReactJS
//Function expression ===> ""
</script>
Practice Paper
1) What is function?
Ans:
2) Set of statements called as
3)Are functions used to reuse business logic?
4)What are the types of functions?
5) Write the Syntax for function declaration?
6) Write the Syntax for function expression?
page. 113 https://www.excelr.com/
ExcelR Frontend by Samba
7) Write the Syntax for arrow functions?
8) Can I store function expression and arrow functions to variables?
9) How to represent arrow functions _
10) Arrow functions are introduced in _ version
11) How to read data and write from <input> tag
12) How to get the Reference of <input type=”text” id=” username”>
Ans
13) How to set the content to HTML Element
14)What are the differences between innerHTML and innerText in JavaScript?
Ans:
15) User Enters first name, Last
name then they will click
fullname. Now full name has to
display in Input3
16) User Enter FirstName, Last
Name Then when they click
fullname then Full name display in
h1 element
17) User Enter a, b values, then
What ever Button they click that
result has to Display in output
area.
18) User Enter a, b values then
they click on Button, now swap
the numbers then display in next
input controls
page. 114 https://excelr.com/
ExcelR Frontend by Samba
19) User Enter annual salary then
They click on calculate button.
Now monthly salary has to
display in TextBox2
20) User Enter p value, q value
Then they click on button. Now
product Has to display in input
control 3
21) User Enters product name,
cost, Quantity. Now if he clicks
calculate button Then bill amount
has to display in Input4
22) User Enter 3 subject marks,
then if they click on total it has to
display In Input4, if they click on
Avg then Collect total from
textbox4 and find Average then
displays in Input5
23) User Enter Loan Amount,
Interest rate, time period. Now if
they click on interest Amount It
has to calculate interest amount
and display in Input4, If they click
on Final Amount then It has to
display in Input5
page. 115 https://www.excelr.com/
ExcelR Frontend by Samba
24) User Enter Product name,
Cost, gst %. Now if they click on
GstAmt, GstAmt has to display in
Input4. Now if they click on Final
Cost then Productcost+gstAmt has
to display in Input5.
25) write the answers for below
code
<script>
function func_one (param1, param2, param3) {
document. write (param1, param2, param3, "<br>");
}
func_one("ReactJS","NodeJS","MongoDB");
Ans:
func_one (100,200,300,400);
Ans:
func_one ();
Ans:
func_one(undefined,"Hello");
Ans:
func_one (null, null, null);
Ans:
</script>
26) what is spread operator? Write few points
Ans:
27) how to represent spread operator
28) is spread operator released in ES6 version
29) write the rules to use spread operator?
Ans:
30) what are default parameters in functions?
page. 116 https://www.excelr.com/
ExcelR Frontend by Samba
Ans:
31) can we pass more than one default parameter (yes/no)?
32) will undefined overrides the default value (yes/no)?
33) will null overrides the default value (yes/no)?
34) write the program with function declaration, function expression and arrow
function
➢ name of the function is “demo_func”
➢ should display “welcome to named functions”
➢ call the “demo_func”
35) write the program with function declaration, function expression and arrow
function
✓ name of the function is “demo_func”
✓ should contain following parameters
1)formal parameter with “param1”
2)default parameter with “param2” with value as “Hello”
3)spread operator with “param3”
✓ call the demo_func without any arguments (no data)
36) create the function declaration/function expression/arrow function should
return number “10”
Ans:
37) create the function declaration/function expression/arrow function should
return string “ExcelR”
Ans:
page. 117 https://www.excelr.com/
ExcelR Frontend by Samba
38) create the function declaration/function expression/arrow function should
return Boolean “true”
Ans:
39) create the function declaration/function expression/arrow function should
return [100,200,300,400,500]
Ans:
40) create the function declaration/function expression/arrow function should
return {sub_one: `ReactJS`, sub_two: `NodeJS`, sub_three: `MongoDB`}
Ans:
page. 118 https://www.excelr.com/
ExcelR Frontend by Samba
41) function declaration/function expression/arrow function should return
following array of objects
[{p_id:111, p_name:"p_one”, p_cost:10000},
{p_id:222, p_name:"p_two”, p_cost:20000},
{p_id:333, p_name:"three”, p_cost:30000},
{p_id:444, p_name:"p_four”, p_cost:40000},
{p_id:555, p_name:"p_five”, p_cost:50000}]
Display result in the form of a table
42) function should accept two parameters
If param1 and param2 are “admin” return true otherwise false
And apply ternary operator
43) function should accept one parameter i.e., number Return square of a
Number
44) function should accept one parameter i.e., number Return even / odd
45) function should accept one parameter i.e.; number Return prime number
or not
46) function should accept three parameters
1) param1 is number
2) param2 also number
3) param3 is string (i.e., add / sub / mul / div)
4) return result based on 3rd parameter
FAQ’S
1) Explain Functions in JavaScript?
✓ Particular business logic called as Function
✓ Functions are used to reuse business logic
✓ Types of functions
1)Function declaration / named functions
2)Function Expression / anonymous functions
3)Arrow Functions
2) what are the differences between function declaration and function
expression?
Function Declaration Function Expression
The function with particular name function without name called as
called as Function Declaration function expression
page. 119 https://www.excelr.com/
ExcelR Frontend by Samba
Syntax Syntax
Function Declaration Function Expression
function functionname (param1….) let variablename=function(param1,…) {
{ //business logic
//business logic }
} Function Calling
Function Calling variablename(arg1,…)
functionname(arg1,…)
Function Declarations are Hoisted Function Expressions are not Hoisted
(Loaded in Creation Phase with (Wont load in creation phase)
Function Definition) (Whenever interpreter reached then
(Fully Hoisted) only loads into browser memory)
function declarations in callbacks, function expression in callbacks, not
behaves like global functions behaves like global functions
Ex. Ex.
let arr1= [100,200,300,400,500]; let arr1= [100,200,300,400,500];
arr1.filter(func_one); arr1.filter(function (element, index) {
function func_one (element, index) //business logic
{ });
//business logic
}
where func_one is global function
Function declarations are not Function expressions are suggested for
suggested for IIFE IIFE
Function declarations are not Function expressions are secured
secured
3) Explain Arrow Functions?
✓ Arrow functions are introduced in ES6 version
✓ We will represent Arrow functions with “=>” symbol
✓ Arrow functions reduces code size
✓ return statement is optional for single line function
Ex.
let arrow_func= () =>`ExcelR`;
✓ functional braces “{}” are optional for single line statement
Ex.
let arrow_func= () =>document. write (`ExcelR`);
✓ Arrow functions binds the lexical context
Example:
page. 120 https://www.excelr.com/
ExcelR Frontend by Samba
let obj={
name: ‘ExcelR`,outer_func:
function () {
inner_func = () => {
document. write(this.name);
}
inner_func ();
}
};
obj. outer_func (); //ExcelR
4) Explain IIFE?
✓ IIFE stands for Immediately Invoked Function Expression
✓ IIFE is a JavaScript function that runs as soon as it is defined
Syntax:
(function () {
//business logic
})()
5) differences Between Spread Operator and Rest Parameter?
✓ Both Syntax’s are same (…)
✓ Spread Operator Expands an Array/Object into its Elements
Ex1.
let arr1= [10,20,30,40,50];
let [a, b, c, d, e] =arr1;
Ex2:
let obj= {key1: `ExcelR`, key2: `Technologies`};
let {key1, key2} =obj;
✓ Rest Syntax will take multiple elements and condenses into single
element
Ex1.
function func_one (…rest) {
document.write(rest);
}
func_one (10,20,30,40,50);
6) what is lexical scope?
✓ a variable defined outside a function can be accessible inside another
function defined after the variable declaration
✓ reverse not possible
page. 121 https://www.excelr.com/