Difference between revisions of "JavaScript/C2/Functions-in-JS/English"
Nancyvarkey (Talk | contribs) |
Pravin1389 (Talk | contribs) |
||
Line 599: | Line 599: | ||
'''on JS''' | '''on JS''' | ||
− | || Here, if we see the output '''Spoken Tutorials on JS '''is being '''logged''' from the '''d1, | + | || Here, if we see the output '''Spoken Tutorials on JS '''is being '''logged''' from the '''d1, d2''' and '''d3 functions'''. |
|- | |- |
Revision as of 11:21, 3 February 2021
Title of the script:Functions
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer:
Keywords: JavaScript, HTML, Function, value, arguments, parameters
|
|
Slide: Title | Hello and Welcome to the spoken tutorial on “Functions in JS”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However you may use any other browser of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Functions |
|
Slide: Types of Functions | The different types of functions are,
Let us take an example and understand this better. |
Switch to Visual Studio code editor | Switch to the Visual Studio code editor. |
[Editor]
Open Folder -> Practice-JS |
In the Welcome page, click on the Open folder link at the top left corner.
|
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named index.html |
[Editor] Type:
<head> <title>Functions</title> </head> <body> <script src="main.js"></script> </body> </html> |
In the index.html file, type the code as shown.
|
Press: Ctrl + S | Save the file. |
Press: Alt + L and Alt + O
|
Now press the key combinations Alt + L and Alt + O.
|
Show firefox | The default browser will open automatically and a new tab opens. |
[Firefox] Press Ctrl + Shift + I
|
Now press Ctrl + Shift + I keys together.
|
[Firefox] Click on the Console tab | Now click on the console tab in the developer tools. |
Switch to Editor | Switch back to the editor. |
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named main.js |
[Editor] Type:
console.log(1+2); return true; }
console.log(add()); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
In JS, to create a basic function, we use the function keyword.
|
[Editor] Highlight:
|
Inside the function block, we can have multiple instructions.
|
[Editor] Highlight:
|
Functions always return some value.
|
[Editor] Highlight:
|
Only when the function is called, the function block will get executed.
|
[Editor] Highlight:
|
Here, I have written the add() function inside the console log statement.
|
Press: Ctrl + S | Save the file. |
Switch to browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
true |
Here in the output, we observe:
|
Switch to Editor | Switch back to the editor. |
[Editor] Highlight:
|
The first 3 is printed for the first function call. |
[Editor] Highlight:
|
The second 3 is printed for the second function call. |
[Editor] Highlight:
|
And true is printed because add() function’s return value is logged. |
[Editor] Type:
return a - 1; }
console.log(result); |
Next, let us learn about parameterized function.
|
[Editor] Highlight:
|
Here, we have created a function similar to the basic function.
|
[Editor] Highlight:
|
Here, we are subtracting the value and returning the subtracted value. |
[Editor] Highlight:
|
In this line, the function call is made passing 6 as the parameter.
|
[Editor] Highlight:
console.log(result); |
Here, we are logging the value on the console. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
The output is 5 because we have passed the number 6 as the parameter.
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
return true; };
console.log(result); |
Next, let us learn about the function as a value.
|
[Editor] Highlight:
|
Here, we have declared a variable with the name mul.
|
[Editor] Highlight:
|
We call function () as an anonymous function.
|
[Editor] Highlight:
return true; |
This function just returns true. |
[Editor] Highlight:
|
The function call is similar to the basic and parameterized ones.
|
[Editor] Highlight:
|
Here, we are logging the result variable. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
Here, as the function returns true, "True" is printed on the console. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
console.log('Spoken'); } function d2() { console.log('Tutorials'); } function d3() { console.log('on JS'); } function display(fn, fn1, fn2) { fn(); fn1(); fn2(); }
|
Next, let us learn about function as a parameter.
|
[Editor] Highlight:
console.log('Spoken'); }
console.log('Tutorials'); }
console.log('on JS'); } |
Here, we have declared three functions with names d1, d2 and d3 respectively. |
[Editor] Highlight:
|
Here, I have created another function named display.
|
[Editor] Highlight:
fn1(); fn2(); |
Inside, the display function,we’re making function calls for the parameters.
|
[Editor] Highlight:
|
While making the display function call, we’re passing 3 functions d1, d2 and d3 respectively.
|
Slide: callback function | Callback function is defined as a function which is passed as a parameter to another function. |
[Editor] Highlight:
|
d1, d2 and d3 will be taken as fn, fn1, fn2 in display function as the parameters.
|
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
Tutorials on JS |
Here, if we see the output Spoken Tutorials on JS is being logged from the d1, d2 and d3 functions. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
marks: function () { return 100; }, };
console.log(result); |
Next, let us learn about function as a property.
|
[Editor] Highlight:
var student = {}; |
Here, student is the object. |
[Editor] Highlight:
return 100; }, |
Here, marks is a property in the student object, which holds an anonymous function. |
[Editor] Highlight:
|
This function returns 100. |
[Editor] Highlight:
|
To make a function call, we need to call the property inside the student object.
|
console.log(result); | Here, we are logging the result. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
Here, the output is 100 as the function returns 100. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } }
|
Next, let us understand about function arguments.
|
[Editor] Highlight:
|
So far, when we passed parameters to a function, we declared a parameter variable while creating the function.
|
[Editor] Highlight:
|
And we have not declared any variables to catch the values passed in the marks function. |
[Editor] Highlight:
} |
Using the keyword arguments, we can access the parameters being passed.
|
[Editor] Highlight:
|
Here, we are just logging the value in the arguments. |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight: 87 78 90 81 |
As we are passing 87, 78, 90, 81 as parameters, the output we get is 87, 78, 90, 81. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
this.name = stuName; this.age = stuAge; }
var s2 = new Student("S2", 11); console.log(s1); console.log(s2); |
Next, let us understand the constructor function.
|
[Editor] Highlight:
|
Here, we have created a function named Student and passing the parameters stuName and stuAge.
|
[Editor] Highlight:
this.age = stuAge; |
Inside the function block we have assigned, this.name as stuName and this.age as stuAge.
|
[Editor] Highlight:
var s2 = new Student("S2", 11); |
Here, we have made the function calls,
|
Only narration | The constructor function creates a new object with help of parameters and returns the object.
|
[Editor] Highlight:
console.log(s2); |
Here, we are logging s1 and s2 |
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
Object { name: "S2", age: 11 } |
Here, we see we have 2 objects printed.
|
Only narration | Hence, we conclude that the constructor function returns an object.
|
With this we have come to the end of this tutorial.
| |
Slide: Summary | In this tutorial, we have learnt about:
|
Slide: Assignment | As an assignment:
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum |
Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education (MoE), Government of India |
Slide: Thanks | The script for this tutorial is contributed by Jayesh.
And this is Praveen from IIT Bombay signing off. Thank you for joining. |